Skip to content
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: AWS support pod mutation #152

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const (
AWSLoadFromSharedConfigAnnotation = WebhookAnnotationPrefix + "aws-load-from-shared-config"
AWSCredentialsNamespaceAnnotation = WebhookAnnotationPrefix + "credentials-namespace"
AWSCredentialsSecretNameAnnotation = WebhookAnnotationPrefix + "credentials-secret-name"
AWSTLSSecretARNAnnotation = WebhookAnnotationPrefix + "aws-tls-secret-arn"
)

// ENVIRONMENT VARIABLES
Expand Down Expand Up @@ -227,10 +228,11 @@ const (
BaoSATokenVolumeNameEnvVar = "bao_service_account_token_volume_name"

// AWS environment variables
AWSRegionEnvVar = "AWS_REGION"
AWSLoadFromSharedConfigEnvVar = "AWS_LOAD_FROM_SHARED_CONFIG"
AWSCredentialsNamespaceEnvVar = "AWS_CREDENTIALS_NAMESPACE"
AWSCredentialsSecretNameEnvVar = "AWS_CREDENTIALS_SECRET_NAME"
AWSRegionEnvVar = "aws_region"
AWSLoadFromSharedConfigEnvVar = "aws_load_from_shared_config"
AWSCredentialsNamespaceEnvVar = "aws_credentials_namespace"
AWSCredentialsSecretNameEnvVar = "aws_credentials_secret_name"
AWSTLSSecretARNEnvVar = "aws_tls_secret_arn"
)

// DEPRECATED ANNOTATIONS AND ENVIRONMENT VARIABLES
Expand Down
7 changes: 7 additions & 0 deletions pkg/provider/aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
LoadFromSharedConfig bool
CredentialsNamespace string
CredentialsSecretName string
TLSSecretARN string
}

func loadConfig(obj metav1.Object) (Config, error) {
Expand Down Expand Up @@ -71,5 +72,11 @@ func loadConfig(obj metav1.Object) (Config, error) {
config.CredentialsSecretName = defaultCredentialsSecretName
}

if val, ok := annotations[common.AWSTLSSecretARNAnnotation]; ok {
config.TLSSecretARN = val
} else {
config.TLSSecretARN = viper.GetString(common.AWSTLSSecretARNEnvVar)
}

return config, nil
}
23 changes: 16 additions & 7 deletions pkg/provider/aws/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,29 @@ func (m *mutator) createAWSSession(ctx context.Context, k8sClient kubernetes.Int
}

func (m *mutator) createSessionUsingK8sSecretCredentials(ctx context.Context, k8sClient kubernetes.Interface) (session.Options, error) {
secret, err := k8sClient.CoreV1().Secrets(m.config.CredentialsNamespace).Get(
ctx,
m.config.CredentialsSecretName,
metav1.GetOptions{},
)
secret, err := m.getK8sSecretCredentials(ctx, k8sClient)
if err != nil {
return session.Options{}, fmt.Errorf("failed to get AWS credentials secret: %w", err)
return session.Options{}, fmt.Errorf("failed to get Kubernetes secret credentials: %w", err)
}

return session.Options{
SharedConfigState: session.SharedConfigDisable,
Config: aws.Config{
Region: aws.String(m.config.Region),
Credentials: credentials.NewStaticCredentials(string(secret.Data["AWS_ACCESS_KEY_ID"]), string(secret.Data["AWS_SECRET_ACCESS_KEY"]), ""),
Credentials: credentials.NewStaticCredentials(string(secret["AWS_ACCESS_KEY_ID"]), string(secret["AWS_SECRET_ACCESS_KEY"]), ""),
},
}, nil
}

func (m *mutator) getK8sSecretCredentials(ctx context.Context, k8sClient kubernetes.Interface) (map[string][]byte, error) {
secret, err := k8sClient.CoreV1().Secrets(m.config.CredentialsNamespace).Get(
ctx,
m.config.CredentialsSecretName,
metav1.GetOptions{},
)
if err != nil {
return nil, fmt.Errorf("failed to get AWS credentials secret: %w", err)
}

return secret.Data, nil
}
Loading
Loading