Skip to content

Commit

Permalink
Merge pull request #47 from vshn/fix_user_passwords
Browse files Browse the repository at this point in the history
add minio server's api call to set user's password
  • Loading branch information
wejdross authored Jul 19, 2024
2 parents 2e83541 + f04fba4 commit 450ab65
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ __debug*

# work
/.work/
.vscode/
16 changes: 13 additions & 3 deletions operator/minioutil/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,29 @@ import (
// It can be used to assign a policy to a usser.
func NewMinioAdmin(ctx context.Context, c client.Client, config *providerv1.ProviderConfig) (*madmin.AdminClient, error) {

secret, tls, parsed, err := ExtractDataFromProviderConfig(ctx, c, config)
if err != nil {
return nil, err
}

return madmin.New(parsed.Host, string(secret.Data[MinioIDKey]), string(secret.Data[MinioSecretKey]), tls)
}

// this is the helper function that is used in the NewMinioClient function
func ExtractDataFromProviderConfig(ctx context.Context, c client.Client, config *providerv1.ProviderConfig) (*corev1.Secret, bool, *url.URL, error) {
secret := &corev1.Secret{}
key := client.ObjectKey{Name: config.Spec.Credentials.APISecretRef.Name, Namespace: config.Spec.Credentials.APISecretRef.Namespace}
err := c.Get(ctx, key, secret)
if err != nil {
return nil, err
return nil, false, nil, err
}

parsed, err := url.Parse(config.Spec.MinioURL)
if err != nil {
return nil, err
return nil, false, nil, err
}

tls := isTLSEnabled(parsed)

return madmin.New(parsed.Host, string(secret.Data[MinioIDKey]), string(secret.Data[MinioSecretKey]), tls)
return secret, tls, parsed, nil
}
20 changes: 16 additions & 4 deletions operator/user/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package user
import (
"context"
"fmt"
"net/url"

"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
Expand All @@ -26,8 +27,11 @@ type connector struct {
}

type userClient struct {
ma *madmin.AdminClient
recorder event.Recorder
ma *madmin.AdminClient
kube client.Client
recorder event.Recorder
url *url.URL
tlsSettings bool
}

func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
Expand All @@ -54,9 +58,17 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E
return nil, err
}

_, tls, parsed, err := minioutil.ExtractDataFromProviderConfig(ctx, c.kube, config)
if err != nil {
return nil, err
}

uc := &userClient{
ma: ma,
recorder: c.recorder,
ma: ma,
kube: c.kube,
recorder: c.recorder,
url: parsed,
tlsSettings: tls,
}

return uc, nil
Expand Down
35 changes: 35 additions & 0 deletions operator/user/observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/minio/madmin-go/v3"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
miniov1 "github.com/vshn/provider-minio/apis/minio/v1"
k8svi "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)

const (
Expand All @@ -18,6 +23,7 @@ const (
)

func (u *userClient) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {
log := ctrl.LoggerFrom(ctx)

user, ok := mg.(*miniov1.User)
if !ok {
Expand Down Expand Up @@ -59,6 +65,35 @@ func (u *userClient) Observe(ctx context.Context, mg resource.Managed) (managed.
user.SetConditions(miniov1.Disabled())
}

if mg.GetDeletionTimestamp() == nil {

secret := k8svi.Secret{}

err = u.kube.Get(ctx, types.NamespacedName{
Namespace: mg.GetWriteConnectionSecretToReference().Namespace,
Name: mg.GetWriteConnectionSecretToReference().Name,
}, &secret)
if err != nil {
return managed.ExternalObservation{}, err
}

mclient, err := minio.New(u.url.Host, &minio.Options{
Creds: credentials.NewStaticV4(string(secret.Data[AccessKeyName]), string(secret.Data[SecretKeyName]), ""),
Secure: u.tlsSettings,
})
if err != nil {
return managed.ExternalObservation{ResourceUpToDate: false, ResourceExists: true}, nil
}

_, err = mclient.ListBuckets(context.Background())
// AccessDenied is ok in this context, because we just want to check if the user has working credentials
if err != nil && err.Error() != "Access Denied." {
return managed.ExternalObservation{ResourceUpToDate: false, ResourceExists: true}, nil
}

log.Info("user client created, everything went fine " + string(secret.Data[AccessKeyName]) + " " + string(secret.Data[SecretKeyName]))
}

return managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true}, nil
}

Expand Down
21 changes: 21 additions & 0 deletions operator/user/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/minio/madmin-go/v3"

miniov1 "github.com/vshn/provider-minio/apis/minio/v1"
k8svi "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
controllerruntime "sigs.k8s.io/controller-runtime"
)

Expand Down Expand Up @@ -48,6 +51,24 @@ func (u *userClient) Update(ctx context.Context, mg resource.Managed) (managed.E
return managed.ExternalUpdate{}, err
}

if mg.GetDeletionTimestamp() == nil {

secret := k8svi.Secret{}

err = u.kube.Get(ctx, types.NamespacedName{
Namespace: mg.GetWriteConnectionSecretToReference().Namespace,
Name: mg.GetWriteConnectionSecretToReference().Name,
}, &secret)
if err != nil {
return managed.ExternalUpdate{}, err
}

err = u.ma.SetUser(ctx, string(secret.Data[AccessKeyName]), string(secret.Data[SecretKeyName]), madmin.AccountEnabled)
if err != nil {
return managed.ExternalUpdate{}, err
}
}

u.emitUpdateEvent(user)
return managed.ExternalUpdate{}, nil
}
Expand Down
8 changes: 7 additions & 1 deletion test/e2e/upload-object.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ access_key=$(kubectl -n "${secret_namespace}" get secret "${secret_name}" -o jso
secret_key=$(kubectl -n "${secret_namespace}" get secret "${secret_name}" -o jsonpath='{.data.AWS_SECRET_ACCESS_KEY}' | base64 -d)
export MC_HOST_minio=http://${access_key}:${secret_key}@${endpoint}

"${GOBIN}/mc" cp --quiet "${file_path}" "minio/${bucket_name}"
echo "Uploading object to bucket: ${bucket_name}"
echo "File path: ${file_path}"
echo "Endpoint: ${endpoint}"
echo "Access key: ${access_key}"
echo "Secret key: ${secret_key}"

"${GOBIN}/mc" cp --quiet --debug "${file_path}" "minio/${bucket_name}"

0 comments on commit 450ab65

Please sign in to comment.