-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bence Csati <[email protected]>
- Loading branch information
Showing
12 changed files
with
354 additions
and
40 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
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,52 @@ | ||
// Copyright © 2024 Bank-Vaults Maintainers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package aws | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/spf13/viper" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/bank-vaults/secrets-webhook/pkg/common" | ||
) | ||
|
||
type Config struct { | ||
ObjectNamespace string | ||
Region string | ||
LoadFromSharedConfig bool | ||
} | ||
|
||
func loadConfig(obj metav1.Object) (Config, error) { | ||
config := Config{ | ||
ObjectNamespace: obj.GetNamespace(), | ||
} | ||
|
||
annotations := obj.GetAnnotations() | ||
|
||
if val, ok := annotations[common.AWSRegionAnnotation]; ok { | ||
config.Region = val | ||
} else { | ||
config.Region = viper.GetString(common.AWSRegionEnvVar) | ||
} | ||
|
||
if val, ok := annotations[common.AWSLoadFromSharedConfigAnnotation]; ok { | ||
config.LoadFromSharedConfig, _ = strconv.ParseBool(val) | ||
} else { | ||
config.LoadFromSharedConfig = viper.GetBool(common.AWSLoadFromSharedConfigEnvVar) | ||
} | ||
|
||
return config, 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,25 @@ | ||
// Copyright © 2024 Bank-Vaults Maintainers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/bank-vaults/secrets-webhook/pkg/provider" | ||
) | ||
|
||
func (m *mutator) MutateConfigMap(_ context.Context, _ provider.ConfigMapMutateRequest) error { | ||
return 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,104 @@ | ||
// Copyright © 2024 Bank-Vaults Maintainers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/secretsmanager" | ||
"github.com/aws/aws-sdk-go/service/ssm" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
type mutator struct { | ||
client *client | ||
config *Config | ||
logger *slog.Logger | ||
} | ||
|
||
type client struct { | ||
smClient *secretsmanager.SecretsManager | ||
ssmClient *ssm.SSM | ||
} | ||
|
||
func (m *mutator) newClient(ctx context.Context, k8sClient kubernetes.Interface) error { | ||
sess, err := m.createAWSSession(ctx, k8sClient) | ||
if err != nil { | ||
return fmt.Errorf("failed to create AWS session: %w", err) | ||
} | ||
|
||
m.client = &client{ | ||
smClient: secretsmanager.New(sess), | ||
ssmClient: ssm.New(sess), | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *mutator) createAWSSession(ctx context.Context, k8sClient kubernetes.Interface) (*session.Session, error) { | ||
// Loading session data from shared config is disabled by default and needs to be | ||
// explicitly enabled via AWS_LOAD_FROM_SHARED_CONFIG | ||
options := session.Options{ | ||
SharedConfigState: session.SharedConfigDisable, | ||
Config: aws.Config{ | ||
Region: aws.String(m.config.Region), | ||
}, | ||
} | ||
|
||
// Enable loading session data from mounted .aws directory | ||
if m.config.LoadFromSharedConfig { | ||
options.SharedConfigState = session.SharedConfigEnable | ||
} else { | ||
// Create session using Kubernetes secret credentials | ||
var err error | ||
options, err = m.createSessionUsingK8sSecretCredentials(ctx, k8sClient) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create session using Kubernetes secret credentials: %w", err) | ||
} | ||
} | ||
|
||
// Create session | ||
sess, err := session.NewSessionWithOptions(options) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create AWS session: %w", err) | ||
} | ||
|
||
return sess, nil | ||
} | ||
|
||
func (m *mutator) createSessionUsingK8sSecretCredentials(ctx context.Context, k8sClient kubernetes.Interface) (session.Options, error) { | ||
secret, err := k8sClient.CoreV1().Secrets("default").Get( | ||
ctx, | ||
"aws-credentials", | ||
metav1.GetOptions{}, | ||
) | ||
if err != nil { | ||
return session.Options{}, fmt.Errorf("failed to get secret: %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"]), ""), | ||
}, | ||
}, 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,25 @@ | ||
// Copyright © 2024 Bank-Vaults Maintainers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/bank-vaults/secrets-webhook/pkg/provider" | ||
) | ||
|
||
func (m *mutator) MutateObject(_ context.Context, _ provider.ObjectMutateRequest) error { | ||
return 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,34 @@ | ||
// Copyright © 2024 Bank-Vaults Maintainers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
appCommon "github.com/bank-vaults/secrets-webhook/pkg/common" | ||
"github.com/bank-vaults/secrets-webhook/pkg/provider" | ||
"github.com/bank-vaults/secrets-webhook/pkg/registry" | ||
) | ||
|
||
func (m *mutator) MutatePod(_ context.Context, _ provider.PodMutateRequest) error { | ||
return nil | ||
} | ||
|
||
func (m *mutator) MutateContainers(_ context.Context, _ []corev1.Container, _ *corev1.PodSpec, _ appCommon.Config, _ appCommon.SecretInitConfig, _ kubernetes.Interface, _ registry.ImageRegistry) (bool, error) { | ||
return false, nil | ||
} |
Oops, something went wrong.