Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K8SPSMDB-1164: Allow creating user with $external database #1690

Merged
merged 27 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
281ea28
Update crds.
inelpandzic Oct 30, 2024
6e85b24
Generate custom user pass.
inelpandzic Oct 30, 2024
b91a8a1
Update e2e test.
inelpandzic Oct 30, 2024
2e90cb9
Fix check
inelpandzic Oct 30, 2024
f204fad
Remove check
inelpandzic Oct 30, 2024
1780fb8
Log
inelpandzic Oct 30, 2024
5dec493
Fix
inelpandzic Oct 30, 2024
b9c3932
Propper pass key.
inelpandzic Oct 30, 2024
579342f
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Oct 30, 2024
1de4b2c
Refactor and fix external db user creation.
inelpandzic Oct 30, 2024
7eeb1ba
Update e2e test.
inelpandzic Oct 30, 2024
75c1edc
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Nov 1, 2024
79d8181
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Nov 13, 2024
0c83355
Update e2e tests.
inelpandzic Nov 13, 2024
0bc1914
Make sure if external DB user is created that we don't handle user
inelpandzic Nov 13, 2024
42e16f1
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Nov 13, 2024
965cc40
Fix custom-users-roles-sharded test.
inelpandzic Nov 14, 2024
5e4d011
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Nov 14, 2024
1c33033
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Nov 17, 2024
5330f06
Order roles for getUser.
inelpandzic Nov 21, 2024
9be37ca
Fix getRole order.
inelpandzic Nov 21, 2024
9a99799
Fix getRole roles order.
inelpandzic Nov 21, 2024
a4fa535
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Nov 21, 2024
6ea2dda
Refactor
inelpandzic Nov 21, 2024
d05e2e1
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Nov 22, 2024
6394cce
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Nov 25, 2024
e7ddd29
Merge branch 'main' into K8SPSMDB-1164-external-user-db
inelpandzic Dec 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18764,7 +18764,6 @@ spec:
type: array
required:
- name
- passwordSecretRef
- roles
type: object
type: array
Expand Down
1 change: 0 additions & 1 deletion deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19460,7 +19460,6 @@ spec:
type: array
required:
- name
- passwordSecretRef
- roles
type: object
type: array
Expand Down
1 change: 0 additions & 1 deletion deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19460,7 +19460,6 @@ spec:
type: array
required:
- name
- passwordSecretRef
- roles
type: object
type: array
Expand Down
1 change: 0 additions & 1 deletion deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19460,7 +19460,6 @@ spec:
type: array
required:
- name
- passwordSecretRef
- roles
type: object
type: array
Expand Down
20 changes: 20 additions & 0 deletions e2e-tests/custom-users-roles-sharded/compare/user-external.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
switched to db $external
{
"_id" : "$external.user-external",
"user" : "user-external",
"db" : "$external",
"roles" : [
{
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"external"
]
}
bye
8 changes: 4 additions & 4 deletions e2e-tests/custom-users-roles-sharded/compare/user-five.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ switched to db testAdmin
"user" : "user-five",
"db" : "testAdmin",
"roles" : [
{
"role" : "role-four",
"db" : "testAdmin1"
},
{
"role" : "role-five",
"db" : "testAdmin2"
},
{
"role" : "role-four",
"db" : "testAdmin1"
}
],
"mechanisms" : [
Expand Down
21 changes: 21 additions & 0 deletions e2e-tests/custom-users-roles-sharded/compare/user-gen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
switched to db admin
{
"_id" : "admin.user-gen",
"user" : "user-gen",
"db" : "admin",
"roles" : [
{
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
bye
4 changes: 2 additions & 2 deletions e2e-tests/custom-users-roles-sharded/compare/user-two.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ switched to db admin
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "clusterAdmin",
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
Expand Down
14 changes: 14 additions & 0 deletions e2e-tests/custom-users-roles-sharded/conf/some-name-rs0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ spec:
db: admin
- name: userAdminAnyDatabase
db: admin
- name: user-gen
db: admin
roles:
- name: clusterAdmin
db: admin
- name: userAdminAnyDatabase
db: admin
- name: user-external
db: $external
roles:
- name: clusterAdmin
db: admin
- name: userAdminAnyDatabase
db: admin

backup:
enabled: false
Expand Down
97 changes: 67 additions & 30 deletions e2e-tests/custom-users-roles-sharded/run
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,43 @@ check_auth() {
fi
}

get_user_cmd() {
local user="$1"

cmd="(function() {
var user = db.getUser(${user});
var roles = user.roles;
roles.sort((a, b) => {
if (a.role < b.role) return -1;
if (a.role > b.role) return 1;
return 0;
});
user.roles = roles;
printjson(user);
})();"

echo "$cmd"
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change

get_role_cmd() {
local role="$1"

cmd="(function() {
var role = db.getRole(${role}, {showPrivileges: true, showAuthenticationRestrictions: true});
var roles = role.roles;
roles.sort((a, b) => {
if (a.role < b.role) return -1;
if (a.role > b.role) return 1;
return 0;
});
role.roles = roles;
printjson(role);
})();"

echo "$cmd"
}

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions
set_debug
Expand Down Expand Up @@ -75,9 +112,18 @@ desc 'check user created on cluster creation'

userOne="user-one"
userOnePass=$(getSecretData "user-one" "userOnePassKey")
compare 'admin' 'db.getUser("user-one")' "$mongosUri" "user-one"
compare 'admin' "$(get_user_cmd \"user-one\")" "$mongosUri" "user-one"
check_auth "$userOne:$userOnePass@$cluster-mongos.$namespace"

generatedUserSecret="$cluster-custom-user-secret"
generatedPass=$(kubectl_bin get secret $generatedUserSecret -o jsonpath="{.data.user-gen}" | base64 -d)
compare 'admin' "$(get_user_cmd \"user-gen\")" "$mongosUri" "user-gen"
check_auth "user-gen:$generatedPass@$cluster-mongos.$namespace"

# Only check if $external.user-external user exists, as the password is not known
# since we don't have a external provider set in this test
compare '$external' "$(get_user_cmd \"user-external\")" "$mongosUri" "user-external"

desc 'delete initial user from CR and create a new one'
kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
"spec": {"users":[
Expand All @@ -97,7 +143,7 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
}'
wait_for_running $cluster-rs0 3

compare 'admin' 'db.getUser("user-two")' "$mongosUri" "user-two"
compare 'admin' "$(get_user_cmd \"user-two\")" "$mongosUri" "user-two"

userTwo="user-two"
userTwoPass=$(getSecretData "user-two" "userTwoPassKey")
Expand Down Expand Up @@ -131,22 +177,22 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
}'
wait_for_running $cluster-rs0 3

compare 'admin' 'db.getUser("user-two")' "$mongosUri" "user-two-update-roles"
compare 'admin' "$(get_user_cmd \"user-two\")" "$mongosUri" "user-two-update-roles"

desc 'check user roles update from DB'

run_mongos \
'use admin\n db.updateUser("user-two", { roles : [{ role : "userAdminAnyDatabase", db: "admin"}]})' \
"$mongosUri"
sleep 15
compare 'admin' 'db.getUser("user-two")' "$mongosUri" "user-two-update-roles"
compare 'admin' "$(get_user_cmd \"user-two\")" "$mongosUri" "user-two-update-roles"

desc 'check user recreated after deleted from DB'
run_mongos \
'use admin\n db.dropUser("user-two")' \
"$mongosUri"
sleep 15
compare 'admin' 'db.getUser("user-two")' "$mongosUri" "user-two-update-roles"
compare 'admin' "$(get_user_cmd \"user-two\")" "$mongosUri" "user-two-update-roles"

desc 'check new user created after updated user name via CR'
kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
Expand All @@ -166,8 +212,8 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
}'
wait_for_running $cluster-rs0 3

compare 'admin' 'db.getUser("user-three")' "$mongosUri" "user-three-admin-db"
compare 'admin' 'db.getUser("user-two")' "$mongosUri" "user-two-update-roles"
compare 'admin' "$(get_user_cmd \"user-three\")" "$mongosUri" "user-three-admin-db"
compare 'admin' "$(get_user_cmd \"user-two\")" "$mongosUri" "user-two-update-roles"

# user-three and user-two should be in the DB
check_auth "$userTwo:$userTwoNewPass@$cluster-mongos.$namespace"
Expand All @@ -191,8 +237,8 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
}'
wait_for_running $cluster-rs0 3

compare 'newDb' 'db.getUser("user-three")' "$mongosUri" "user-three-newDb-db"
compare 'admin' 'db.getUser("user-three")' "$mongosUri" "user-three-admin-db"
compare 'newDb' "$(get_user_cmd \"user-three\")" "$mongosUri" "user-three-newDb-db"
compare 'admin' "$(get_user_cmd \"user-three\")" "$mongosUri" "user-three-admin-db"

desc 'check new user created with default db and secret password key'
kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
Expand All @@ -210,21 +256,19 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
}'
wait_for_running $cluster-rs0 3

compare 'admin' 'db.getUser("user-four")' "$mongosUri" "user-four"
compare 'admin' "$(get_user_cmd \"user-four\")" "$mongosUri" "user-four"

# ======================== Roles ========================

desc 'check user role on cluster initialization'
compare 'admin' 'db.getRole("role-one", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-one"
compare 'admin' "$(get_role_cmd \"role-one\")" "$mongosUri" "role-one"

desc 'check role recreated after deleted from DB'
run_mongos \
'use admin\n db.dropRole("role-one")' \
"$mongosUri"
sleep 15
compare 'admin' 'db.getRole("role-one", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-one"
compare 'admin' "$(get_role_cmd \"role-one\" )" "$mongosUri" "role-one"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
compare 'admin' "$(get_role_cmd \"role-one\" )" "$mongosUri" "role-one"
compare 'admin' "$(get_role_cmd \"role-one\")" "$mongosUri" "role-one"


desc 'delete initial role from CR and create a new one'
kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
Expand Down Expand Up @@ -255,10 +299,8 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{

wait_for_running $cluster-rs0 3

compare 'admin' 'db.getRole("role-one", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-one"
compare 'admin' 'db.getRole("role-two", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-two"
compare 'admin' "$(get_role_cmd \"role-one\" )" "$mongosUri" "role-one"
compare 'admin' "$(get_role_cmd \"role-two\" )" "$mongosUri" "role-two"
Comment on lines +302 to +303
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
compare 'admin' "$(get_role_cmd \"role-one\" )" "$mongosUri" "role-one"
compare 'admin' "$(get_role_cmd \"role-two\" )" "$mongosUri" "role-two"
compare 'admin' "$(get_role_cmd \"role-one\")" "$mongosUri" "role-one"
compare 'admin' "$(get_role_cmd \"role-two\")" "$mongosUri" "role-two"


desc 'check role update from CR'
kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
Expand All @@ -282,16 +324,14 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
}}'
wait_for_running $cluster-rs0 3

compare 'admin' 'db.getRole("role-two", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-two-updated"
compare 'admin' "$(get_role_cmd \"role-two\" )" "$mongosUri" "role-two-updated"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
compare 'admin' "$(get_role_cmd \"role-two\" )" "$mongosUri" "role-two-updated"
compare 'admin' "$(get_role_cmd \"role-two\")" "$mongosUri" "role-two-updated"


desc 'check role update from DB'
run_mongos \
'use admin\n db.updateRole( "role-two",{privileges:[{resource: {db:"config", collection:"" }, actions: ["find", "update"]}]})' \
"$mongosUri"
sleep 15
compare 'admin' 'db.getRole("role-two", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-two-updated"
compare 'admin' "$(get_role_cmd \"role-two\" )" "$mongosUri" "role-two-updated"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
compare 'admin' "$(get_role_cmd \"role-two\" )" "$mongosUri" "role-two-updated"
compare 'admin' "$(get_role_cmd \"role-two\")" "$mongosUri" "role-two-updated"


desc 'check new role created after updated role name via CR'
kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
Expand All @@ -315,8 +355,7 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
}}'
wait_for_running $cluster-rs0 3

compare 'admin' 'db.getRole("role-three", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-three"
compare 'admin' "$(get_role_cmd \"role-three\" )" "$mongosUri" "role-three"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
compare 'admin' "$(get_role_cmd \"role-three\" )" "$mongosUri" "role-three"
compare 'admin' "$(get_role_cmd \"role-three\")" "$mongosUri" "role-three"


desc 'check creating multiple roles and the users in a single CR apply'
kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
Expand Down Expand Up @@ -433,12 +472,10 @@ kubectl_bin patch psmdb ${cluster} --type=merge --patch '{
]
}}'
wait_for_running $cluster-rs0 3
compare 'testAdmin1' 'db.getRole("role-four", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-four"
compare 'testAdmin2' 'db.getRole("role-five", {showPrivileges: true, showAuthenticationRestrictions: true})' \
"$mongosUri" "role-five"
compare 'testAdmin' 'db.getUser("user-five")' "$mongosUri" "user-five"
compare 'testAdmin' 'db.getUser("user-six")' "$mongosUri" "user-six"
compare 'testAdmin1' "$(get_role_cmd \"role-four\" )" "$mongosUri" "role-four"
compare 'testAdmin2' "$(get_role_cmd \"role-five\" )" "$mongosUri" "role-five"
Comment on lines +475 to +476
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
compare 'testAdmin1' "$(get_role_cmd \"role-four\" )" "$mongosUri" "role-four"
compare 'testAdmin2' "$(get_role_cmd \"role-five\" )" "$mongosUri" "role-five"
compare 'testAdmin1' "$(get_role_cmd \"role-four\")" "$mongosUri" "role-four"
compare 'testAdmin2' "$(get_role_cmd \"role-five\")" "$mongosUri" "role-five"

compare 'testAdmin' "$(get_user_cmd \"user-five\")" "$mongosUri" "user-five"
compare 'testAdmin' "$(get_user_cmd \"user-six\")" "$mongosUri" "user-six"

destroy $namespace

Expand Down
20 changes: 20 additions & 0 deletions e2e-tests/custom-users-roles/compare/user-external.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
switched to db $external
{
"_id" : "$external.user-external",
"user" : "user-external",
"db" : "$external",
"roles" : [
{
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"external"
]
}
bye
8 changes: 4 additions & 4 deletions e2e-tests/custom-users-roles/compare/user-five.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ switched to db testAdmin
"user" : "user-five",
"db" : "testAdmin",
"roles" : [
{
"role" : "role-four",
"db" : "testAdmin1"
},
{
"role" : "role-five",
"db" : "testAdmin2"
},
{
"role" : "role-four",
"db" : "testAdmin1"
}
],
"mechanisms" : [
Expand Down
21 changes: 21 additions & 0 deletions e2e-tests/custom-users-roles/compare/user-gen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
switched to db admin
{
"_id" : "admin.user-gen",
"user" : "user-gen",
"db" : "admin",
"roles" : [
{
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
bye
Loading
Loading