Skip to content

Commit 246accd

Browse files
Eneman DonatienDonatien26
authored andcommitted
[FIX] 🐛 Fix bug when reconcile policy for user
1 parent 72a5617 commit 246accd

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

internal/controller/policy/finalizer.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ limitations under the License.
1717
package policy_controller
1818

1919
import (
20-
"bytes"
2120
"context"
22-
"encoding/json"
2321

24-
"github.com/minio/madmin-go/v3"
2522
ctrl "sigs.k8s.io/controller-runtime"
2623
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2724
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -107,25 +104,3 @@ func (r *PolicyReconciler) handleDeletion(
107104
}
108105
return ctrl.Result{}, nil
109106
}
110-
111-
func (r *PolicyReconciler) isPolicyMatchingWithCustomResource(
112-
policyResource *s3v1alpha1.Policy,
113-
effectivePolicy *madmin.PolicyInfo,
114-
) (bool, error) {
115-
// The policy content visible in the custom resource usually contains indentations and newlines
116-
// while the one we get from S3 is compacted. In order to compare them, we compact the former.
117-
policyResourceAsByteSlice := []byte(policyResource.Spec.PolicyContent)
118-
buffer := new(bytes.Buffer)
119-
err := json.Compact(buffer, policyResourceAsByteSlice)
120-
if err != nil {
121-
return false, err
122-
}
123-
124-
// Another gotcha is that the effective policy comes up as a json.RawContent,
125-
// which needs marshalling in order to be properly compared to the []byte we get from the CR.
126-
marshalled, err := json.Marshal(effectivePolicy.Policy)
127-
if err != nil {
128-
return false, err
129-
}
130-
return bytes.Equal(buffer.Bytes(), marshalled), nil
131-
}

internal/controller/policy/reconcile.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package policy_controller
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"encoding/json"
2123
"fmt"
2224

2325
k8sapierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -29,6 +31,7 @@ import (
2931
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3032

3133
s3v1alpha1 "github.com/InseeFrLab/s3-operator/api/v1alpha1"
34+
"github.com/minio/madmin-go/v3"
3235
)
3336

3437
// Reconcile is part of the main kubernetes reconciliation loop which aims to
@@ -387,3 +390,27 @@ func (r *PolicyReconciler) handleCreation(ctx context.Context, req reconcile.Req
387390
err,
388391
)
389392
}
393+
394+
func (r *PolicyReconciler) isPolicyMatchingWithCustomResource(
395+
policyResource *s3v1alpha1.Policy,
396+
effectivePolicy *madmin.PolicyInfo,
397+
) (bool, error) {
398+
// The policy content visible in the custom resource usually contains indentations and newlines
399+
// while the one we get from S3 is compacted. In order to compare them, we compact the former.
400+
401+
policyResourceAsByteSlice := []byte(policyResource.Spec.PolicyContent)
402+
buffer := new(bytes.Buffer)
403+
err := json.Compact(buffer, policyResourceAsByteSlice)
404+
if err != nil {
405+
return false, err
406+
}
407+
408+
// Another gotcha is that the effective policy comes up as a json.RawContent,
409+
// which needs marshalling in order to be properly compared to the []byte we get from the CR.
410+
marshalled, err := json.Marshal(effectivePolicy.Policy)
411+
if err != nil {
412+
return false, err
413+
}
414+
415+
return bytes.Equal(buffer.Bytes(), marshalled), nil
416+
}

internal/s3/client/impl/minioS3Client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"net/http"
2626
neturl "net/url"
27+
"slices"
2728
"strings"
2829

2930
s3client "github.com/InseeFrLab/s3-operator/internal/s3/client"
@@ -468,6 +469,9 @@ func (minioS3Client *MinioS3Client) GetUserPolicies(accessKey string) ([]string,
468469

469470
return []string{}, err
470471
}
472+
if len(strings.Split(userInfo.PolicyName, ",")) == 1 && slices.Contains(strings.Split(userInfo.PolicyName, ","), "") {
473+
return []string{}, nil
474+
}
471475
return strings.Split(userInfo.PolicyName, ","), nil
472476
}
473477

0 commit comments

Comments
 (0)