-
Notifications
You must be signed in to change notification settings - Fork 55
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
feat: use aws secret manager #128
Closed
Closed
Changes from all commits
Commits
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 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
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
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 |
---|---|---|
|
@@ -7,14 +7,15 @@ import ( | |
"github.com/scratchdata/scratchdata/config" | ||
"github.com/scratchdata/scratchdata/pkg/storage/database/memory" | ||
"github.com/scratchdata/scratchdata/pkg/storage/database/models" | ||
"github.com/scratchdata/scratchdata/pkg/storage/vault" | ||
) | ||
|
||
type Database interface { | ||
VerifyAdminAPIKey(hashedAPIKey string) bool | ||
|
||
GetDestinations() []config.Destination | ||
CreateDestination(destType string, settings map[string]any) (config.Destination, error) | ||
GetDestinationCredentials(dbID int64) (config.Destination, error) | ||
GetStorageServices() []config.StorageService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed. When creating a database, we already pass the StorageServices struct into the |
||
|
||
AddAPIKey(destId int64, hashedAPIKey string) error | ||
GetAPIKeyDetails(hashedAPIKey string) (models.APIKey, error) | ||
|
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,129 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/scratchdata/scratchdata/config" | ||
"github.com/scratchdata/scratchdata/pkg/storage/vault" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/secretsmanager" | ||
"github.com/aws/aws-sdk-go-v2/service/secretsmanager/types" | ||
) | ||
|
||
type AWSVault struct { | ||
AccessKeyId string `mapstructure:"access_key_id"` | ||
SecretAccessKey string `mapstructure | ||
Prefix string `mapstructure:"prefix"` | ||
} | ||
|
||
func NewAWSVault(conf config.Vault) *AWSVault { | ||
return &AWSVault{ | ||
AccessKeyId: conf.AccessKeyId, | ||
SecretAccessKey: conf.SecretAccessKey, | ||
Prefix: conf.Prefix, | ||
} | ||
} | ||
|
||
func (vault *AWSVault) GetSecret(ctx context.Context, key string) (string, error) { | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
client := secretsmanager.NewFromConfig(cfg) | ||
|
||
input := &secretsmanager.GetSecretValueInput{ | ||
SecretId: aws.String(vault.Prefix + key), | ||
} | ||
|
||
result, err := client.GetSecretValue(ctx, input) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return *result.SecretString, nil | ||
} | ||
|
||
func (vault *AWSVault) SetSecret(ctx context.Context, key string, value string) error { | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := secretsmanager.NewFromConfig(cfg) | ||
|
||
input := &secretsmanager.CreateSecretInput{ | ||
Name: aws.String(vault.Prefix + key), | ||
SecretString: aws.String(value), | ||
} | ||
|
||
_, err = client.CreateSecret(ctx, input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (vault *AWSVault) DeleteSecret(ctx context.Context, key string) error { | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := secretsmanager.NewFromConfig(cfg) | ||
|
||
input := &secretsmanager.DeleteSecretInput{ | ||
SecretId: aws.String(vault.Prefix + key), | ||
} | ||
|
||
_, err = client.DeleteSecret(ctx, input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (vault *AWSVault) ListSecrets(ctx context.Context) ([]string, error) { | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := secretsmanager.NewFromConfig(cfg) | ||
|
||
input := &secretsmanager.ListSecretsInput{} | ||
|
||
result, err := client.ListSecrets(ctx, input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var secrets []string | ||
for _, secret := range result.SecretList { | ||
secrets = append(secrets, *secret.Name) | ||
} | ||
|
||
return secrets, nil | ||
} | ||
|
||
func (vault *AWSVault) GetSecrets(ctx context.Context) (map[string]string, error) { | ||
|
||
secrets, err := vault.ListSecrets(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
secretMap := map[string]string{} | ||
for _, secret := range secrets { | ||
value, err := vault.GetSecret(ctx, secret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
secretMap[secret] = value | ||
} | ||
|
||
return secretMap, nil | ||
} |
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,26 @@ | ||
package memory | ||
|
||
import ( | ||
"github.com/scratchdata/scratchdata/config" | ||
) | ||
|
||
func NewMemoryVault(conf config.vault destination []config.Destination, apiKeys []config.APIKey) *MemoryVault { | ||
rc := MemoryVault{ | ||
conf: conf, | ||
destinations: destination, | ||
apiKeyToDestination: map[string]int64{}, | ||
adminAPIKeys: apiKeys | ||
} | ||
|
||
for i, destination := range destinations { | ||
for _, apiKey := range destination.APIKeys { | ||
rc.apiKeyToDestination[apiKey] = int64(i) | ||
} | ||
} | ||
|
||
return &rc | ||
} | ||
|
||
func (vault *MemoryVault) GetDestinationCredentials(vaultID int64) (config.Destination, error) { | ||
return vault.destinations[vaultID], nil | ||
} |
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,23 @@ | ||
package vault | ||
|
||
import ( | ||
"github.com/scratchdata/scratchdata/config" | ||
"github.com/scratchdata/scratchdata/pkg/storage/vault/vault" | ||
"io" | ||
) | ||
|
||
type Vault interface { | ||
GetCredentails(name string) string | ||
SetCredentials(name, value string) | ||
} | ||
|
||
func NewVault(conf config.Vault, destinations []config.Destination, adminKeys []config.APIKey) Vault { | ||
switch conf.Type { | ||
case "memory": | ||
return memory.NewMemoryVault(conf, destinations, adminKeys) | ||
case "aws": | ||
return aws.NewAWSVault(conf) | ||
} | ||
|
||
return nil | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not change the signature of this function. Instead, add a new
Vault
member to themodels.StorageServices
struct. You can follow the same pattern we use for the blobstore and queue in this function.