Skip to content

Commit 768a099

Browse files
feat: added MILVU client to configure access rights
1 parent fb85f47 commit 768a099

File tree

10 files changed

+242
-41
lines changed

10 files changed

+242
-41
lines changed

TaskfileTest.yml

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,62 +120,62 @@ tasks:
120120
echo "*** $APIHOST ***"
121121
AUTH=$(kubectl -n nuvolaris get wsk/controller -o jsonpath='{.spec.openwhisk.namespaces.nuvolaris}')
122122
echo $AUTH
123-
echo wsk property set --apihost $APIHOST --auth $AUTH
124-
wsk property set --apihost $APIHOST --auth $AUTH
125-
while ! wsk action list
123+
echo ops -wsk property set --apihost $APIHOST --auth $AUTH
124+
ops -wsk property set --apihost $APIHOST --auth $AUTH
125+
while ! ops action list
126126
do echo $(( N++)) "waiting for the load balancer to be ready..." ; sleep 10
127127
done
128128
129129
hello:
130-
- wsk -i action update hello tests/hello.js --web=true
131-
- wsk -i action invoke hello -r | grep "hello"
130+
- ops -wsk -i action update hello tests/hello.js --web=true
131+
- ops -wsk -i action invoke hello -r | grep "hello"
132132
- |
133-
URL=$(wsk -i action get hello --url | tail +2)
133+
URL=$(ops -wsk -i action get hello --url | tail +2)
134134
curl -skL $URL | grep hello
135135
136136
redis:
137137
cmds:
138-
- wsk -i package update redis -p redis_url "{{.REDIS_NUV_URL}}" -p redis_prefix "{{.REDIS_NUV_PREFIX}}" -p password "{{.REDIS_PASSWORD}}"
139-
- wsk -i action update redis/ping tests/ping.js
140-
- wsk -i action invoke redis/ping -r | grep "PONG"
141-
- wsk -i action update redis/redis tests/redis.js
142-
- wsk -i action invoke redis/redis -r | grep "world"
138+
- ops -wsk -i package update redis -p redis_url "{{.REDIS_NUV_URL}}" -p redis_prefix "{{.REDIS_NUV_PREFIX}}" -p password "{{.REDIS_PASSWORD}}"
139+
- ops -wsk -i action update redis/ping tests/ping.js
140+
- ops -wsk -i action invoke redis/ping -r | grep "PONG"
141+
- ops -wsk -i action update redis/redis tests/redis.js
142+
- ops -wsk -i action invoke redis/redis -r | grep "world"
143143
vars:
144144
REDIS_NUV_PREFIX:
145145
sh: kubectl -n nuvolaris get cm/config -o jsonpath='{.metadata.annotations.redis_prefix}'
146146
REDIS_NUV_URL:
147147
sh: kubectl -n nuvolaris get cm/config -o jsonpath='{.metadata.annotations.redis_url}'
148148

149149
echo:
150-
- wsk -i action update echo tests/echo.js -a provide-api-key true
151-
- wsk -i action invoke echo -r | grep "__OW_API_KEY"
152-
- wsk -i action invoke echo -r | grep "__OW_API_HOST"
150+
- ops -wsk -i action update echo tests/echo.js -a provide-api-key true
151+
- ops -wsk -i action invoke echo -r | grep "__OW_API_KEY"
152+
- ops -wsk -i action invoke echo -r | grep "__OW_API_HOST"
153153

154154
api:
155-
- wsk -i action update api tests/api.js -a provide-api-key true
156-
- wsk -i action invoke api -r | grep '"api"'
155+
- ops -wsk -i action update api tests/api.js -a provide-api-key true
156+
- ops -wsk -i action invoke api -r | grep '"api"'
157157

158158
mongo:
159159
- |
160160
MONGODB_URL=$(kubectl -n nuvolaris get cm/config -o jsonpath='{.metadata.annotations.mongodb_url}')
161161
wsk -i package update mongo -p dburi "$MONGODB_URL"
162-
- wsk -i action update mongo/mongo tests/mongo.js
163-
- wsk -i action invoke mongo/mongo -r | grep "hello"
162+
- ops -wsk -i action update mongo/mongo tests/mongo.js
163+
- ops -wsk -i action invoke mongo/mongo -r | grep "hello"
164164

165165
minio:
166-
- wsk -i package update minio -p minio_host {{.MINIO_HOST}} -p minio_port {{.MINIO_PORT}} -p minio_user {{.MINIO_USER}} -p minio_pwd {{.MINIO_PWD}}
167-
- wsk -i action update minio/minio tests/minio.js
168-
- wsk -i action invoke minio/minio -r
166+
- ops -wsk -i package update minio -p minio_host {{.MINIO_HOST}} -p minio_port {{.MINIO_PORT}} -p minio_user {{.MINIO_USER}} -p minio_pwd {{.MINIO_PWD}}
167+
- ops -wsk -i action update minio/minio tests/minio.js
168+
- ops -wsk -i action invoke minio/minio -r
169169

170170
mongo2:
171-
- wsk -i project deploy --manifest tests/mongo.yaml
171+
- ops -wsk -i project deploy --manifest tests/mongo.yaml
172172

173173
postgres:
174174
- |
175175
PG_URL=$(kubectl -n nuvolaris get cm/config -o jsonpath='{.metadata.annotations.postgres_url}')
176-
wsk -i package update postgres -p dburi "$PG_URL"
177-
- wsk -i action update postgres/postgres tests/postgres.js
178-
- wsk -i action invoke postgres/postgres -r | grep "Nuvolaris Postgres is up and running!"
176+
ops -wsk -i package update postgres -p dburi "$PG_URL"
177+
- ops -wsk -i action update postgres/postgres tests/postgres.js
178+
- ops -wsk -i action invoke postgres/postgres -r | grep "Nuvolaris Postgres is up and running!"
179179

180180
minio2:
181181
silent: true
@@ -188,10 +188,10 @@ tasks:
188188
MINIO_PORT=$(kubectl -n nuvolaris get cm/config -o jsonpath='{.metadata.annotations.s3_port}')
189189
MINIO_BUCKET_DATA=$(kubectl -n nuvolaris get cm/config -o jsonpath='{.metadata.annotations.s3_bucket_data}')
190190
MINIO_BUCKET_WEB=$(kubectl -n nuvolaris get cm/config -o jsonpath='{.metadata.annotations.s3_bucket_static}')
191-
wsk -i action update minio/minio-nuv tests/minio-nuv.js \
191+
ops -wsk -i action update minio/minio-nuv tests/minio-nuv.js \
192192
-p minio_access "$MINIO_ACCESS_KEY" \
193193
-p minio_secret "$MINIO_SECRET_KEY" \
194194
-p minio_host "$MINIO_HOST" \
195195
-p minio_port "$MINIO_PORT" \
196196
-p minio_data "$MINIO_BUCKET_DATA"
197-
- wsk -i action invoke minio/minio-nuv -r
197+
- ops -wsk -i action invoke minio/minio-nuv -r

deploy/nginx-static/nginx-static-sts.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ spec:
3636
initContainers:
3737
- name: check-minio
3838
image: busybox:1.36.0
39-
command: ['sh', '-c', "until nslookup minio.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for minio; sleep 2; done"]
39+
command: ['sh', '-c', "until nslookup nuvolaris-minio.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for minio; sleep 2; done"]
4040
containers:
4141
- name: nuvolaris-static
4242
image: nginxinc/nginx-unprivileged:1.24

deploy/nuvolaris-permissions/whisk-crd.yaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,15 +738,28 @@ spec:
738738
description: MILVUS related user passwords
739739
type: object
740740
properties:
741+
root:
742+
description: milvus root password
743+
type: string
741744
etcd:
742745
description: ETCD milvus user password
743746
type: string
744747
s3:
745748
description: MINIO/S3 milvus user password
746749
type: string
747750
required:
748-
- etcd
749-
- s3
751+
- root
752+
- s3
753+
nuvolaris:
754+
description: MILVUS default user setup
755+
type: object
756+
properties:
757+
password:
758+
description: nuvolaris MILVUS password
759+
type: string
760+
collection:
761+
description: nuvolaris MILVUS collection (default to nuvolaris)
762+
type: string
750763
status:
751764
x-kubernetes-preserve-unknown-fields: true
752765
# type: object

nuvolaris/milvus_admin_client.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
import logging
19+
import nuvolaris.config as cfg
20+
from pymilvus import MilvusClient
21+
22+
class MilvusAdminClient:
23+
"""
24+
Simple Milvus Client used to perform Mivlus administration Tasks
25+
"""
26+
def __init__(self, db_name="default"):
27+
self.admin_username = cfg.get("milvus.admin.user", "MILVUS_ROOT_USER", "root")
28+
self.milvus_api_host = cfg.get("milvus.host", "MILVUS_API_HOST", "nuvolaris-milvus")
29+
self.milvus_api_port = cfg.get("milvus.host", "MILVUS_API_PORT", "19530")
30+
self.admin_password = cfg.get("milvus.password.root", "MILVUS_ROOT_PASSWORD", "An0therPa55")
31+
32+
self.milvus_api_url = f"http://{self.milvus_api_host}:{self.milvus_api_port}"
33+
self.client = MilvusClient(
34+
uri=self.milvus_api_url,
35+
token=f"{self.admin_username}:{self.admin_password}",
36+
db_name=db_name
37+
)
38+
39+
print(f"{self.admin_username}:{self.admin_password}")
40+
41+
def close_connection(self):
42+
try:
43+
self.client.close()
44+
logging.info("MILVUS client connection closed")
45+
except Exception as ex:
46+
logging.warning("cannot close MILVUS client connection", ex)
47+
48+
def add_user(self, username, password):
49+
"""
50+
adds a new MILVUS user to the predefined
51+
param: username
52+
param: password
53+
return: True if user has been successfully created
54+
"""
55+
try:
56+
self.client.create_user(username, password)
57+
created_user = self.client.describe_user(username)
58+
return 'user_name' in created_user
59+
except Exception as ex:
60+
logging.error(f"Could not create milvus user {username}", ex)
61+
return False
62+
63+
def add_role(self, role):
64+
"""
65+
adds a new MILVUS role
66+
param: role
67+
return: True if role has been successfully created
68+
"""
69+
try:
70+
self.client.create_role(role)
71+
created_role = self.client.describe_role(role)
72+
return 'role_name' in created_role
73+
except Exception as ex:
74+
logging.error(f"Could not create milvus role {role}", ex)
75+
return False
76+
77+
def add_default_privileges_to_role(self, role):
78+
"""
79+
adds default privileges to a role
80+
param: role
81+
return: True if role has been successfully created
82+
"""
83+
try:
84+
self.client.grant_privilege(
85+
role_name=role,
86+
object_type='Global', # value here can be Global, Collection or User, object type also depends on the API defined in privilegeName
87+
object_name='*', # value here can be * or a specific user name if object type is 'User'
88+
privilege='CreateCollection'
89+
)
90+
91+
self.client.grant_privilege(
92+
role_name=role,
93+
object_type='Collection', # value here can be Global, Collection or User, object type also depends on the API defined in privilegeName
94+
object_name='*', # value here can be * or a specific user name if object type is 'User'
95+
privilege='*'
96+
)
97+
98+
return True
99+
except Exception as ex:
100+
logging.error(f"Could not create milvus role {role}", ex)
101+
return False
102+
103+
def assign_role(self, username, role):
104+
"""
105+
assign a tole to a user
106+
param: username
107+
param: role
108+
return: True if role has been successfully created
109+
"""
110+
try:
111+
self.client.grant_role(user_name=username,role_name=role)
112+
user_detail = self.client.describe_user(username)
113+
return role in user_detail['roles']
114+
except Exception as ex:
115+
logging.error(f"Could not assign MILVUS role {role}", ex)
116+
return False
117+
118+
def setup_user(self, username, password):
119+
"""
120+
Creates a user into MILVUS and assign permission on collection
121+
param: username
122+
param: role
123+
return: True if role has been successfully created
124+
"""
125+
role = f"{username}_role"
126+
self.add_user(username,password)
127+
self.add_role(role)
128+
self.add_default_privileges_to_role(role)
129+
self.assign_role(username, role)

nuvolaris/templates/milvus-cfg-base.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,9 @@ data:
132132
# serverKeyPath: /etc/milvus/certs/tls.key
133133
# common:
134134
# security:
135-
# tlsMode: 1
135+
# tlsMode: 1
136+
# Enable User Authentication
137+
common:
138+
security:
139+
authorizationEnabled: true
140+
defaultRootPassword: {{milvus_root_password}}

nuvolaris/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,10 @@ def get_milvus_config_data():
726726
'replicas': cfg.get('milvus.replicas') or 1,
727727
'storageClass': cfg.get('nuvolaris.storageclass'),
728728
"etcd_replicas":get_etcd_replica(),
729-
"etcd_container": "nuvolaris-etcd",
729+
"etcd_container": "nuvolaris-etcd",
730+
'milvus_root_password': cfg.get('milvus.password.root') or "An0therPa55",
731+
'nuvolaris_password': cfg.get('milvus.nuvolaris.password') or "Nuv0therPa55",
732+
'nuvolaris_collection': cfg.get('milvus.nuvolaris.collection') or "nuvolaris"
730733
}
731734

732735
data["etcd_range"]=range(data["etcd_replicas"])

tests/k3s/whisk.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ spec:
191191
journal: 20
192192
ledgers: 25
193193
replicas: 1
194-
password:
194+
password:
195+
root: x£VqD7G6712o
195196
etcd: 97Vk2{qe8o>S
196197
s3: 8_d$8zCrl7£m

tests/kind/milvus_admin_test.ipy

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
import os
21+
import nuvolaris.testutil as tu
22+
import nuvolaris.config as cfg
23+
from nuvolaris.milvus_admin_client import MilvusAdminClient
24+
from pymilvus import MilvusClient
25+
26+
# for this test minioClient should see this env variable
27+
os.environ['MILVUS_ROOT_USER']='root'
28+
os.environ['MILVUS_API_HOST']='localhost'
29+
os.environ['MILVUS_API_PORT']='19530'
30+
os.environ['MILVUS_ROOT_PASSWORD']='x£VqD7G6712o'
31+
32+
#client = MilvusAdminClient()
33+
#client.setup_user("franz","Afrodite1972#123")
34+
#client.setup_user("ciccillo","Afrodite1973#123")
35+
#client.close_connection()
36+
37+
milvus = MilvusClient(uri="http://localhost:19530",token="root:x£VqD7G6712o")
38+
print(milvus.grant_privilege(role_name="ciccillo_role",object_type='Global', object_name='*', privilege='CreateCollection'))
39+
#print(milvus.grant_privilege(role_name="franz_role",object_type='Global', object_name='*', privilege='CreateCollection'))
40+
#print(milvus.grant_role(user_name="franz",role_name="franz_role"))
41+
#print(milvus.describe_role(role_name="franz_role"))
42+
#print(milvus.describe_user("franz"))
43+
milvus.close()
44+
45+
fttclient = MilvusClient(uri="http://localhost:19530",token="franz:Afrodite1972#123")
46+
#print(fttclient.create_collection(collection_name="franz_collection", dimension=100))
47+
fttclient.close()
48+
49+
cclient = MilvusClient(uri="http://localhost:19530",token="ciccillo:Afrodite1973#123")
50+
print(cclient.create_collection(collection_name="cc_collection",dimension=100))
51+
print(cclient.list_collections())
52+

tests/kind/milvus_standalone_test.ipy

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ assert(cfg.detect_storage()["nuvolaris.storageclass"])
3636
# for this test minioClient should see this env variable
3737
os.environ['MINIO_API_HOST']='localhost'
3838

39-
# for this test minioClient should see this env variable
40-
os.environ['MINIO_API_HOST']='localhost'
41-
4239
assert(etcd.create())
4340
assert(minio.create())
4441
assert(milvus.create())

tests/kind/whisk.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ spec:
3333
# start mongodb
3434
mongodb: false
3535
# start redis
36-
redis: true
36+
redis: false
3737
# start cron based action parser
38-
cron: true
38+
cron: false
3939
# tls enabled or not
4040
tls: false
4141
# minio enabled or not
@@ -47,7 +47,7 @@ spec:
4747
# etcd enabled or not
4848
etcd: true
4949
# milvus enabled or not
50-
etcd: true
50+
milvus: true
5151
openwhisk:
5252
namespaces:
5353
whisk-system: 789c46b1-71f6-4ed5-8c54-816aa4f8c502:abczO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP
@@ -170,6 +170,7 @@ spec:
170170
journal: 20
171171
ledgers: 25
172172
replicas: 1
173-
password:
173+
password:
174+
root: x£VqD7G6712o
174175
etcd: 97Vk2{qe8o>S
175-
s3: 8_d$8zCrl7£m
176+
s3: 8_d$8zCrl7£m

0 commit comments

Comments
 (0)