Skip to content

Commit

Permalink
add banned-by label to BannedUser CRs (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored Jun 18, 2024
1 parent 88dface commit b539753
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 14 additions & 3 deletions pkg/cmd/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ only one parameter which is the name of the UserSignup to be used for banning`,
}
}

const BannedByLabel = toolchainv1alpha1.LabelKeyPrefix + "banned-by"

func Ban(ctx *clicontext.CommandContext, args ...string) error {
return CreateBannedUser(ctx, args[0], func(userSignup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) {
if _, exists := bannedUser.Labels[toolchainv1alpha1.BannedUserPhoneNumberHashLabelKey]; !exists {
Expand Down Expand Up @@ -63,13 +65,21 @@ func CreateBannedUser(ctx *clicontext.CommandContext, userSignupName string, con
return err
}

bannedUser, err := newBannedUser(userSignup)
ksctlConfig, err := configuration.Load(ctx)
if err != nil {
return err
}

bannedUser, err := newBannedUser(userSignup, ksctlConfig.Name)
if err != nil {
return err
}

emailHashLabelMatch := runtimeclient.MatchingLabels(map[string]string{
toolchainv1alpha1.BannedUserEmailHashLabelKey: bannedUser.Labels[toolchainv1alpha1.BannedUserEmailHashLabelKey],
})
bannedUsers := &toolchainv1alpha1.BannedUserList{}
if err := cl.List(context.TODO(), bannedUsers, runtimeclient.MatchingLabels(bannedUser.Labels), runtimeclient.InNamespace(cfg.OperatorNamespace)); err != nil {
if err := cl.List(context.TODO(), bannedUsers, emailHashLabelMatch, runtimeclient.InNamespace(cfg.OperatorNamespace)); err != nil {
return err
}

Expand All @@ -93,7 +103,7 @@ func CreateBannedUser(ctx *clicontext.CommandContext, userSignupName string, con
return nil
}

func newBannedUser(userSignup *toolchainv1alpha1.UserSignup) (*toolchainv1alpha1.BannedUser, error) {
func newBannedUser(userSignup *toolchainv1alpha1.UserSignup, bannedBy string) (*toolchainv1alpha1.BannedUser, error) {
var emailHashLbl, phoneHashLbl string
var exists bool

Expand All @@ -111,6 +121,7 @@ func newBannedUser(userSignup *toolchainv1alpha1.UserSignup) (*toolchainv1alpha1
GenerateName: "banneduser-",
Labels: map[string]string{
toolchainv1alpha1.BannedUserEmailHashLabelKey: emailHashLbl,
BannedByLabel: bannedBy,
},
},
Spec: toolchainv1alpha1.BannedUserSpec{
Expand Down
8 changes: 5 additions & 3 deletions pkg/test/banneduser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func AssertBannedUser(t *testing.T, fakeClient *test.FakeClient, userSignup *too
err := fakeClient.List(context.TODO(), bannedUsers, runtimeclient.InNamespace(userSignup.Namespace))
require.NoError(t, err)
require.Len(t, bannedUsers.Items, 1)
assert.Equal(t, userSignup.Spec.IdentityClaims.Email, bannedUsers.Items[0].Spec.Email)
assert.Equal(t, userSignup.Labels[toolchainv1alpha1.UserSignupUserEmailHashLabelKey], bannedUsers.Items[0].Labels[toolchainv1alpha1.BannedUserEmailHashLabelKey])
assert.Equal(t, userSignup.Labels[toolchainv1alpha1.UserSignupUserPhoneHashLabelKey], bannedUsers.Items[0].Labels[toolchainv1alpha1.BannedUserPhoneNumberHashLabelKey])
bannedUser := bannedUsers.Items[0]
assert.Equal(t, userSignup.Spec.IdentityClaims.Email, bannedUser.Spec.Email)
assert.Equal(t, userSignup.Labels[toolchainv1alpha1.UserSignupUserEmailHashLabelKey], bannedUser.Labels[toolchainv1alpha1.BannedUserEmailHashLabelKey])
assert.Equal(t, userSignup.Labels[toolchainv1alpha1.UserSignupUserPhoneHashLabelKey], bannedUser.Labels[toolchainv1alpha1.BannedUserPhoneNumberHashLabelKey])
assert.Equal(t, "john", bannedUser.Labels[toolchainv1alpha1.LabelKeyPrefix+"banned-by"])
}

func AssertNoBannedUser(t *testing.T, fakeClient *test.FakeClient, userSignup *toolchainv1alpha1.UserSignup) {
Expand Down

0 comments on commit b539753

Please sign in to comment.