generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,206 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Running the Auth Microservice | ||
|
||
## Prerequisites | ||
|
||
A few secrets are required to run the Auth which are not committed to source. These values are | ||
configured in Okta. | ||
|
||
| Environment variable | Value | | ||
|----------------------|---------------------------------| | ||
| OKTA_ADMIN_CLIENT_API_ENCODED_PRIVATE_KEY | Base 64 encoded private key pem | | ||
| SPRING_SECURITY_OAUTH2_RESOURCESERVER_OPAQUETOKEN_CLIENT_SECRET | Base 64 encoded secret | | ||
|
||
## How to run application locally | ||
|
||
```bash | ||
# from project root | ||
# start ReportStream and all dependent docker containers | ||
./gradlew quickRun | ||
# start submissions service | ||
./ gradlew submissions:bootRun | ||
# start auth service | ||
./gradlew auth:bootRun | ||
``` | ||
|
||
## Setup a Sender | ||
|
||
- Sign in to Admin Okta | ||
- Applications -> Application tab | ||
- Click "Create App Integration" | ||
- Select "API Services" and click next | ||
- Name your sender | ||
- Copy your client ID and client secret or private key locally to be used while calling the /token endpoint | ||
- Add the user to the appropriate sender group | ||
- You can find this option on the small gear next to your newly created application | ||
- Ensure the group has the prefix DHSender_ | ||
|
||
## Submitting reports locally | ||
|
||
- Retrieve an access token directly from Okta and ensure the JWT contains the "sender" scope | ||
- Make a well-formed request to https://reportstream.oktapreview.com/oauth2/default/v1/token to retrieve your access token | ||
- [See Okta documentation on that endpoint here](https://developer.okta.com/docs/guides/implement-oauth-for-okta-serviceapp/main/#get-an-access-token) | ||
- Submit your report to http://localhost:9000/api/v1/reports | ||
- Note the it's port 9000 which is auth rather than directly to 8880 which is submissions | ||
- See endpoint definition in [SubmissionController](../../submissions/src/main/kotlin/gov/cdc/prime/reportstream/submissions/controllers/SubmissionController.kt) | ||
- Add the access token you retrieved from Okta as a `Bearer` token in the `Authorization` header | ||
- Inspect the logs if you received a 401 or a 403. This indicates there is an issue with your access token. | ||
|
||
## Notes on secrets | ||
|
||
The Okta-Groups JWT signing key pair has a local dev value already set up appropriately in auth and | ||
downstream in submissions. New values _must_ be generated for deployed environments. You can look | ||
at [KeyGenerationUtils](../src/test/kotlin/gov/cdc/prime/reportstream/auth/util/KeyGenerationUtils.kt) | ||
for scripts to run to generate new keys. | ||
|
||
By Default, we are connecting to the Staging Okta. We cannot post connection secrets directly in this document so | ||
you will have to ask someone for those values. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
auth/src/main/kotlin/gov/cdc/prime/reportstream/auth/client/OktaGroupsClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package gov.cdc.prime.reportstream.auth.client | ||
|
||
import com.okta.sdk.resource.api.ApplicationGroupsApi | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.apache.logging.log4j.kotlin.Logging | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class OktaGroupsClient( | ||
private val applicationGroupsApi: ApplicationGroupsApi, | ||
) : Logging { | ||
|
||
/** | ||
* Get all application groups from the Okta Admin API | ||
* | ||
* Group names are found at json path "_embedded.group.profile.name" | ||
* | ||
* @see https://developer.okta.com/docs/api/openapi/okta-management/management/tag/ApplicationGroups/#tag/ApplicationGroups/operation/listApplicationGroupAssignments | ||
*/ | ||
suspend fun getApplicationGroups(appId: String): List<String> { | ||
return withContext(Dispatchers.IO) { | ||
try { | ||
val groups = applicationGroupsApi | ||
.listApplicationGroupAssignments(appId, null, null, null, "group") | ||
.map { it.embedded?.get("group") as Map<*, *> } | ||
.map { it["profile"] as Map<*, *> } | ||
.map { it["name"] as String } | ||
logger.info("$appId is a member of ${groups.joinToString()}") | ||
groups | ||
} catch (ex: Exception) { | ||
logger.error("Error retrieving application groups from Okta API", ex) | ||
throw ex | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
auth/src/main/kotlin/gov/cdc/prime/reportstream/auth/config/OktaClientConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package gov.cdc.prime.reportstream.auth.config | ||
|
||
import com.okta.sdk.client.AuthorizationMode | ||
import com.okta.sdk.client.Clients | ||
import com.okta.sdk.resource.api.ApplicationGroupsApi | ||
import com.okta.sdk.resource.client.ApiClient | ||
import gov.cdc.prime.reportstream.shared.StringUtilities.base64Decode | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
|
||
@Configuration | ||
@Profile("!test") | ||
class OktaClientConfig( | ||
private val oktaClientProperties: OktaClientProperties, | ||
) { | ||
|
||
@Bean | ||
fun apiClient(): ApiClient { | ||
return Clients.builder() | ||
.setOrgUrl(oktaClientProperties.orgUrl) | ||
.setAuthorizationMode(AuthorizationMode.PRIVATE_KEY) | ||
.setClientId(oktaClientProperties.clientId) | ||
.setScopes(oktaClientProperties.requiredScopes) | ||
.setPrivateKey(oktaClientProperties.apiPrivateKey) | ||
// .setCacheManager(...) TODO: investigate caching since groups don't often change | ||
.build() | ||
} | ||
|
||
@Bean | ||
fun applicationGroupsApi(): ApplicationGroupsApi { | ||
return ApplicationGroupsApi(apiClient()) | ||
} | ||
|
||
@ConfigurationProperties(prefix = "okta.admin-client") | ||
data class OktaClientProperties( | ||
val orgUrl: String, | ||
val clientId: String, | ||
val requiredScopes: Set<String>, | ||
private val apiEncodedPrivateKey: String, | ||
) { | ||
// PEM encoded format | ||
val apiPrivateKey = apiEncodedPrivateKey.base64Decode() | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
auth/src/main/kotlin/gov/cdc/prime/reportstream/auth/config/RouteConfig.kt
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
auth/src/main/kotlin/gov/cdc/prime/reportstream/auth/config/SubmissionsConfig.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ain/kotlin/gov/cdc/prime/reportstream/auth/filter/AppendOktaGroupsGatewayFilterFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package gov.cdc.prime.reportstream.auth.filter | ||
|
||
import gov.cdc.prime.reportstream.auth.AuthApplicationConstants | ||
import gov.cdc.prime.reportstream.auth.service.OktaGroupsService | ||
import gov.cdc.prime.reportstream.shared.auth.jwt.OktaGroupsJWTConstants | ||
import kotlinx.coroutines.reactor.mono | ||
import org.springframework.cloud.gateway.filter.GatewayFilter | ||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory | ||
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication | ||
import org.springframework.stereotype.Component | ||
import reactor.core.publisher.Mono | ||
|
||
/** | ||
* This filter defines how the Okta-Groups header is added to requests. It follows the conventions | ||
* defined in spring-cloud-gateway and is instantiated via configuration under a route's filters. | ||
*/ | ||
@Component | ||
class AppendOktaGroupsGatewayFilterFactory( | ||
private val oktaGroupsService: OktaGroupsService, | ||
) : AbstractGatewayFilterFactory<Any>() { | ||
|
||
/** | ||
* function used only in testing to create our filter without any configuration | ||
*/ | ||
fun apply(): GatewayFilter { | ||
return apply { _: Any? -> } | ||
} | ||
|
||
override fun apply(config: Any?): GatewayFilter { | ||
return GatewayFilter { exchange, chain -> | ||
exchange | ||
.getPrincipal<BearerTokenAuthentication>() | ||
.flatMap { oktaAccessTokenJWT -> | ||
val appId = oktaAccessTokenJWT | ||
.tokenAttributes[AuthApplicationConstants.Scopes.SUBJECT_SCOPE] as String | ||
val organizations = oktaAccessTokenJWT | ||
.tokenAttributes[AuthApplicationConstants.Scopes.ORGANIZATION_SCOPE] as List<*>? | ||
|
||
// If there is no organization claim present, then we have an application user and | ||
// require appending our custom header | ||
if (organizations == null) { | ||
mono { oktaGroupsService.generateOktaGroupsJWT(appId) } | ||
} else { | ||
Mono.empty() | ||
} | ||
} | ||
.map { oktaGroupsJWT: String -> | ||
exchange.request | ||
.mutate() | ||
.headers { | ||
it.add(OktaGroupsJWTConstants.OKTA_GROUPS_HEADER, oktaGroupsJWT) | ||
} | ||
.build() | ||
} | ||
.switchIfEmpty(Mono.just(exchange.request)) // drop back in original unmodified request if not an app | ||
.flatMap { request -> | ||
chain.filter(exchange.mutate().request(request).build()) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.