mirror of
https://github.com/storytold/irsa-manager.git
synced 2026-10-09 00:09:43 +00:00
aws client injection
This commit is contained in:
+17
-14
@@ -13,36 +13,43 @@ import (
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
)
|
||||
|
||||
type AwsConfig struct {
|
||||
type AwsClientFactory struct {
|
||||
config aws.Config
|
||||
}
|
||||
|
||||
func NewAwsClient(ctx context.Context, region string) (*AwsConfig, error) {
|
||||
cfg, err := config.LoadDefaultConfig(ctx,
|
||||
config.WithRegion(region),
|
||||
type AwsClient interface {
|
||||
IamCient() *AwsIamClient
|
||||
S3Cient(region, bucketName string) *AwsS3Client
|
||||
}
|
||||
|
||||
func NewAwsClientFactory(ctx context.Context) (*AwsClientFactory, error) {
|
||||
cfg, err := config.LoadDefaultConfig(
|
||||
ctx,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to load SDK config, %w", err)
|
||||
}
|
||||
return &AwsConfig{config: cfg}, nil
|
||||
return &AwsClientFactory{config: cfg}, nil
|
||||
}
|
||||
|
||||
func (a *AwsConfig) IamCient() *AwsIamClient {
|
||||
func (a *AwsClientFactory) IamCient() *AwsIamClient {
|
||||
return &AwsIamClient{
|
||||
iam.NewFromConfig(a.config),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AwsConfig) S3Cient(bucketName string) *AwsS3Client {
|
||||
func (a *AwsClientFactory) S3Cient(bucketName, region string) *AwsS3Client {
|
||||
return &AwsS3Client{
|
||||
bucketName,
|
||||
s3.NewFromConfig(a.config),
|
||||
region,
|
||||
bucketName,
|
||||
}
|
||||
}
|
||||
|
||||
type AwsS3Client struct {
|
||||
bucketName string
|
||||
client *s3.Client
|
||||
region string
|
||||
bucketName string
|
||||
}
|
||||
|
||||
func (a *AwsS3Client) PutObjectPublic(ctx context.Context, key string, body []byte) error {
|
||||
@@ -92,7 +99,7 @@ func (a *AwsS3Client) BucketName() string {
|
||||
}
|
||||
|
||||
func (a *AwsS3Client) Region() string {
|
||||
return a.client.Options().Region
|
||||
return a.region
|
||||
}
|
||||
|
||||
type AwsIamClient struct {
|
||||
@@ -112,7 +119,3 @@ func (a *AwsIamClient) CreateOIDCProvider(ctx context.Context, providerUrl strin
|
||||
}
|
||||
return *result.OpenIDConnectProviderArn, nil
|
||||
}
|
||||
|
||||
func (a *AwsIamClient) Region() string {
|
||||
return a.client.Options().Region
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
|
||||
|
||||
irsav1alpha1 "github.com/kkb0318/irsa-manager/api/v1alpha1"
|
||||
awsclient "github.com/kkb0318/irsa-manager/internal/client"
|
||||
"github.com/kkb0318/irsa-manager/internal/selfhosted"
|
||||
"github.com/kkb0318/irsa-manager/internal/selfhosted/oidc"
|
||||
)
|
||||
@@ -32,7 +33,8 @@ import (
|
||||
// IRSASetupReconciler reconciles a IRSASetup object
|
||||
type IRSASetupReconciler struct {
|
||||
client.Client
|
||||
Scheme *runtime.Scheme
|
||||
Scheme *runtime.Scheme
|
||||
AwsClient awsclient.AwsClient
|
||||
}
|
||||
|
||||
//+kubebuilder:rbac:groups=irsa.kkb0318.github.io,resources=irsasetups,verbs=get;list;watch;create;update;patch;delete
|
||||
@@ -63,11 +65,11 @@ func (r *IRSASetupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
||||
}
|
||||
|
||||
func (r *IRSASetupReconciler) reconcile(ctx context.Context, obj *irsav1alpha1.IRSASetup) error {
|
||||
err := reconcileSelfhosted(ctx, obj)
|
||||
err := reconcileSelfhosted(ctx, obj, r.AwsClient)
|
||||
return err
|
||||
}
|
||||
|
||||
func reconcileSelfhosted(ctx context.Context, obj *irsav1alpha1.IRSASetup) error {
|
||||
func reconcileSelfhosted(ctx context.Context, obj *irsav1alpha1.IRSASetup, awsClient awsclient.AwsClient) error {
|
||||
keyPair, err := selfhosted.CreateKeyPair()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -76,7 +78,7 @@ func reconcileSelfhosted(ctx context.Context, obj *irsav1alpha1.IRSASetup) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
factory, err := newOIDCIdpFactory(ctx, obj, jwk)
|
||||
factory, err := newOIDCIdpFactory(ctx, obj, jwk, awsClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -87,7 +89,7 @@ func reconcileSelfhosted(ctx context.Context, obj *irsav1alpha1.IRSASetup) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func newOIDCIdpFactory(ctx context.Context, obj *irsav1alpha1.IRSASetup, jwk *selfhosted.JWK) (selfhosted.OIDCIdPFactory, error) {
|
||||
func newOIDCIdpFactory(ctx context.Context, obj *irsav1alpha1.IRSASetup, jwk *selfhosted.JWK, awsClient awsclient.AwsClient) (selfhosted.OIDCIdPFactory, error) {
|
||||
region := obj.Spec.Discovery.S3.Region
|
||||
bucketName := obj.Spec.Discovery.S3.BucketName
|
||||
jwksFileName := "keys.json"
|
||||
@@ -97,6 +99,7 @@ func newOIDCIdpFactory(ctx context.Context, obj *irsav1alpha1.IRSASetup, jwk *se
|
||||
bucketName,
|
||||
jwk,
|
||||
jwksFileName,
|
||||
awsClient,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -10,20 +10,22 @@ import (
|
||||
type AwsS3IdPFactory struct {
|
||||
region string
|
||||
bucketName string
|
||||
awsConfig *awsclient.AwsConfig
|
||||
awsClient awsclient.AwsClient
|
||||
jwk *selfhosted.JWK
|
||||
jwksFileName string
|
||||
}
|
||||
|
||||
func NewAwsS3IdpFactory(ctx context.Context, region, bucketName string, jwk *selfhosted.JWK, jwksFileName string) (*AwsS3IdPFactory, error) {
|
||||
awsConfig, err := awsclient.NewAwsClient(ctx, region)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func NewAwsS3IdpFactory(ctx context.Context,
|
||||
region, bucketName string,
|
||||
jwk *selfhosted.JWK,
|
||||
jwksFileName string,
|
||||
awsClient awsclient.AwsClient,
|
||||
) (*AwsS3IdPFactory, error) {
|
||||
// awsClient, err := awsclient.NewAwsClientFactory(ctx, region)
|
||||
return &AwsS3IdPFactory{
|
||||
region,
|
||||
bucketName,
|
||||
awsConfig,
|
||||
awsClient,
|
||||
jwk,
|
||||
jwksFileName,
|
||||
}, nil
|
||||
@@ -34,11 +36,11 @@ func (f *AwsS3IdPFactory) IssuerMeta() selfhosted.OIDCIssuerMeta {
|
||||
}
|
||||
|
||||
func (f *AwsS3IdPFactory) IdP(i selfhosted.OIDCIssuerMeta) (selfhosted.OIDCIdP, error) {
|
||||
return NewAwsIdP(f.awsConfig, i)
|
||||
return NewAwsIdP(f.awsClient, i)
|
||||
}
|
||||
|
||||
func (f *AwsS3IdPFactory) IdPDiscovery() selfhosted.OIDCIdPDiscovery {
|
||||
return NewS3IdPDiscovery(f.awsConfig, f.bucketName)
|
||||
return NewS3IdPDiscovery(f.awsClient, f.region, f.bucketName)
|
||||
}
|
||||
|
||||
func (f *AwsS3IdPFactory) IdPDiscoveryContents(i selfhosted.OIDCIssuerMeta) selfhosted.OIDCIdPDiscoveryContents {
|
||||
|
||||
@@ -12,7 +12,7 @@ type AwsIdP struct {
|
||||
issuerMeta selfhosted.OIDCIssuerMeta
|
||||
}
|
||||
|
||||
func NewAwsIdP(awsConfig *client.AwsConfig, issuerMeta selfhosted.OIDCIssuerMeta) (*AwsIdP, error) {
|
||||
func NewAwsIdP(awsConfig client.AwsClient, issuerMeta selfhosted.OIDCIssuerMeta) (*AwsIdP, error) {
|
||||
iamClient := awsConfig.IamCient()
|
||||
return &AwsIdP{iamClient, issuerMeta}, nil
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ type S3IdPDiscovery struct {
|
||||
|
||||
// NewS3IdPDiscovery initializes a new instance of S3IdPCreator with the specified AWS region and bucket name.
|
||||
// This function attempts to create an AWS client configured for the specified region.
|
||||
func NewS3IdPDiscovery(awsConfig *client.AwsConfig, bucketName string) *S3IdPDiscovery {
|
||||
s3Client := awsConfig.S3Cient(bucketName)
|
||||
func NewS3IdPDiscovery(awsConfig client.AwsClient, region, bucketName string) *S3IdPDiscovery {
|
||||
s3Client := awsConfig.S3Cient(region, bucketName)
|
||||
return &S3IdPDiscovery{s3Client}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user