forked from databrickslabs/overwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
PE-939: AWS Secrets Manager #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9a6559d
Add logging
dyon78 53d4227
Add logging
dyon78 138d443
Add logging
dyon78 cfab7ff
Add logging
dyon78 b28f8b3
Add jar
dyon78 98c58be
Add copying
dyon78 f3e9d68
Add action
dyon78 3df2adb
Fix path
dyon78 6f3ea3f
Initial changes
dyon78 b6bb3eb
Changes
dyon78 98c58bd
Changes
dyon78 b106b4b
tests and doc
dyon78 617ae8f
Remove comments
dyon78 7cb2429
Remove file
dyon78 dca3ef2
Review comments
dyon78 7c3f2f9
Remove comments
dyon78 8732ab1
Review comments
dyon78 709d4b3
Review comments
dyon78 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
43 changes: 43 additions & 0 deletions
43
src/main/scala/com/databricks/labs/overwatch/utils/AwsSecrets.scala
This file contains hidden or 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,43 @@ | ||
| package com.databricks.labs.overwatch.utils | ||
|
|
||
| import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder | ||
| import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest | ||
| import org.apache.log4j.{Level, Logger} | ||
| import org.json4s.DefaultFormats | ||
| import org.json4s.jackson.JsonMethods.parse | ||
|
|
||
| import java.util.Base64 | ||
|
|
||
| object AwsSecrets { | ||
| private val logger: Logger = Logger.getLogger(this.getClass) | ||
|
|
||
| def readApiToken(secretId: String, region: String, apiTokenKey: String = "apiToken"): String = { | ||
| secretValueAsMap(secretId, region) | ||
| .getOrElse(apiTokenKey ,throw new IllegalStateException("apiTokenKey param not found")) | ||
| .asInstanceOf[String] | ||
| } | ||
|
|
||
| def secretValueAsMap(secretId: String, region: String = "us-east-2"): Map[String, Any] = | ||
| parseJsonToMap(readRawSecretFromAws(secretId,region)) | ||
|
|
||
| def readRawSecretFromAws(secretId: String, region: String): String = { | ||
| logger.log(Level.INFO,s"Looking up secret $secretId in AWS Secret Manager") | ||
|
|
||
| val secretsClient = AWSSecretsManagerClientBuilder | ||
| .standard() | ||
| .withRegion(region) | ||
| .build() | ||
| val request = new GetSecretValueRequest().withSecretId(secretId) | ||
| val secretValue = secretsClient.getSecretValue(request) | ||
|
|
||
| if (secretValue.getSecretString != null) | ||
| secretValue.getSecretString | ||
| else | ||
| new String(Base64.getDecoder.decode(secretValue.getSecretBinary).array) | ||
| } | ||
|
|
||
| def parseJsonToMap(jsonStr: String): Map[String, Any] = { | ||
| implicit val formats: DefaultFormats.type = org.json4s.DefaultFormats | ||
| parse(jsonStr).extract[Map[String, Any]] | ||
| } | ||
| } |
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
src/main/scala/com/databricks/labs/overwatch/utils/SecretTools.scala
This file contains hidden or 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,57 @@ | ||
| package com.databricks.labs.overwatch.utils | ||
|
|
||
| import com.databricks.dbutils_v1.DBUtilsHolder.dbutils | ||
| import org.apache.log4j.{Level, Logger} | ||
|
|
||
| /** | ||
| * SecretTools handles common functionality related to working with secrets: | ||
| * 1) Get Databricks API token stored in specified secret | ||
| * 2) Normalize secret structure to be stored at Delta table pipeline_report under inputConfig.tokenSecret nested struct column | ||
| * There are two secret types available now - AWS Secrets Manager, Databricks secrets | ||
| */ | ||
| trait SecretTools[T <: TokenSecretContainer] { | ||
| def getApiToken : String | ||
| def getTargetTableStruct: TokenSecret | ||
| } | ||
|
|
||
| object SecretTools { | ||
| private val logger: Logger = Logger.getLogger(this.getClass) | ||
| type DatabricksTokenSecret = TokenSecret | ||
|
|
||
| private class DatabricksSecretTools(tokenSecret : DatabricksTokenSecret) extends SecretTools[DatabricksTokenSecret] { | ||
| override def getApiToken: String = { | ||
| val scope = tokenSecret.scope | ||
| val key = tokenSecret.key | ||
| val authMessage = s"Executing with Databricks token located in secret, scope=$scope : key=$key" | ||
| logger.log(Level.INFO, authMessage) | ||
| dbutils.secrets.get(scope, key) | ||
| } | ||
|
|
||
| override def getTargetTableStruct: TokenSecret = { | ||
| TokenSecret(tokenSecret.scope,tokenSecret.key) | ||
| } | ||
| } | ||
|
|
||
| private class AwsSecretTools(tokenSecret : AwsTokenSecret) extends SecretTools[AwsTokenSecret] { | ||
| override def getApiToken: String = { | ||
| val secretId = tokenSecret.secretId | ||
| val region = tokenSecret.region | ||
| val tokenKey = tokenSecret.tokenKey | ||
| val authMessage = s"Executing with AWS token located in secret, secretId=$secretId : region=$region : tokenKey=$tokenKey" | ||
| logger.log(Level.INFO, authMessage) | ||
| AwsSecrets.readApiToken(secretId, region, tokenSecret.tokenKey) | ||
| } | ||
|
|
||
| override def getTargetTableStruct: TokenSecret = { | ||
| TokenSecret(tokenSecret.region, tokenSecret.secretId) | ||
| } | ||
| } | ||
|
|
||
| def apply(secretSource: TokenSecretContainer): SecretTools[_] = { | ||
| secretSource match { | ||
| case x: AwsTokenSecret => new AwsSecretTools(x) | ||
| case y: DatabricksTokenSecret => new DatabricksSecretTools(y) | ||
| case _ => throw new IllegalArgumentException(s"${secretSource.toString} not implemented") | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.