Skip to content

Commit fd80bc2

Browse files
authored
Add authorize namespace script (#27)
1 parent f7fe553 commit fd80bc2

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,32 @@ oc -n ibm-common-services apply -f deploy/operator.yaml
154154

155155
oc -n ibm-common-services apply -f deploy/cr.yaml
156156
```
157+
158+
## Authorization and Permissions
159+
160+
The **authorize-namespace.sh** script in the `scripts/` directory is used to set up roles and binding in a target namespace.
161+
162+
The syntax for the command is below:
163+
164+
```
165+
authorize-namespace.sh - Authorize a namespace to be manageable from another namespace through the NamespaceScope operator
166+
167+
SYNTAX:
168+
authorize-namespace.sh [namespace | default current namespace] [-to namespacename | default ibm-common-services] [-delete]
169+
WHERE:
170+
namespace : is the name of the namespace you wish to authorize. This namespace MUST exist
171+
by default the current namespace is assumed
172+
tonamespace : is the name of the namespace that you want to authorize to manage artifacts in this namespace.
173+
This namespace MUST exist. The default is ibm-common-services.
174+
The NamespaceScope CR MUST be define in this namespace with the name namespacescope.
175+
-delete : Removes the ability for the tonamespace to manage artifacts in the namespace.
176+
177+
```
178+
179+
For example, if you want to grant namespace admin permission of `common-service` to the service account in `ibm-common-services` namespace, you can use the following command
180+
181+
```bash
182+
scripts/authorize-namespace.sh common-service
183+
```
184+
185+
**NOTE:** You must have cluster administrator access permissions to execute the command.

scripts/authorize-namespace.sh

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2020 IBM Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# 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, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
#
19+
# Project roles and role bindings to another namespace
20+
#
21+
22+
function help() {
23+
echo "authorize-namespace.sh - Authorize a namespace to be managable from another namespare through the NamespaceScope operator"
24+
echo "SYNTAX:"
25+
echo "authorize-namespace.sh [namespace | default current namespace] [-to namespacename | default ibm-common-services] [-delete]"
26+
echo "WHERE:"
27+
echo " namespace : is the name of the namspece you wish to authorize. This namespace MUST exist, "
28+
echo " by default the current namespace is assumed"
29+
echo " tonamespace : is the name of the namespace that you want to authorize to manage artifacts in this namespace."
30+
echo " This namespace MUST exist. The default is ibm-common-services".
31+
echo " The NamepaceScope CR MUST be define in this namespace with the name namespacescope."
32+
echo " -delete : Removes the ability for the tonamespace to manage artifacts in the namespace."
33+
echo ""
34+
echo "You must be logged into the Openshift cluster from the oc command line"
35+
echo ""
36+
}
37+
38+
#
39+
# MAIN LOGIC
40+
#
41+
42+
TARGETNS=""
43+
TONS="ibm-common-services"
44+
DELETE=0
45+
46+
while (( $# )); do
47+
case "$1" in
48+
-to|--to)
49+
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
50+
TONS=$2
51+
shift 2
52+
else
53+
echo "Error: Argument for $1 is missing" >&2
54+
exit 1
55+
fi
56+
;;
57+
-delete|--delete)
58+
DELETE=1
59+
shift 1
60+
;;
61+
-*|--*=) # unsupported flags
62+
echo "Error: Unsupported flag $1" >&2
63+
help
64+
exit 1
65+
;;
66+
*) # preserve positional arguments
67+
TARGETNS="$TARGETNS $1"
68+
shift
69+
;;
70+
esac
71+
done
72+
73+
#
74+
# Validate parameters
75+
#
76+
77+
if [ -z $TARGETNS ]; then
78+
TARGETNS=$(oc project -q)
79+
if [ $? -ne 0 ]; then
80+
echo "Error: You do not seem to be logged into Openshift" >&2
81+
help
82+
exit 1
83+
fi
84+
fi
85+
86+
COUNT=$(echo $TARGETNS | wc -w)
87+
if [ $COUNT -ne 1 ]; then
88+
echo "Invalid namespace " $TARGETNS >&2
89+
help
90+
exit 1
91+
fi
92+
93+
TARGETNS=${TARGETNS//[[:blank:]]/}
94+
95+
oc get ns $TARGETNS
96+
if [ $? -ne 0 ]; then
97+
echo "Invalid namespace " $TARGETNS >&2
98+
help
99+
exit 1
100+
fi
101+
102+
oc get ns $TONS
103+
if [ $? -ne 0 ]; then
104+
echo "Invalid namespace " $TARGETNS >&2
105+
help
106+
exit 1
107+
fi
108+
109+
if [ "$TARGETNS" == "$TONS" ]; then
110+
echo "Namespace and tonamespace canot be the same namespace."
111+
help
112+
exit 1
113+
fi
114+
115+
if [ $DELETE -eq 1 ]; then
116+
echo "Deleteing authorization of namespace $TARGETNS to $TONS" >&2
117+
else
118+
echo "Authorizing namespace $TARGETNS to $TONS" >&2
119+
fi
120+
121+
#
122+
# Delete permissions and update the list if needed
123+
#
124+
if [ $DELETE -ne 0 ]; then
125+
oc delete role -l projectedfrom=$TONS -n $TARGETNS
126+
oc delete rolebinding -l projectedfrom=$TONS -n $TARGETNS
127+
exit 0
128+
fi
129+
130+
131+
#
132+
# Define a role for service accounts
133+
#
134+
cat <<EOF | oc apply -n $TARGETNS -f -
135+
apiVersion: rbac.authorization.k8s.io/v1
136+
kind: Role
137+
metadata:
138+
name: namespace-scope-client
139+
labels:
140+
projectedfrom: $TONS
141+
rules:
142+
- apiGroups:
143+
- "*"
144+
resources:
145+
- "*"
146+
verbs:
147+
- "*"
148+
EOF
149+
150+
#
151+
# Bind the service account in the TO namespace to the Role in the target namespace
152+
#
153+
cat <<EOF | oc apply -n $TARGETNS -f -
154+
kind: RoleBinding
155+
apiVersion: rbac.authorization.k8s.io/v1
156+
metadata:
157+
name: namespace-scope-binding
158+
labels:
159+
projectedfrom: $TONS
160+
subjects:
161+
- kind: ServiceAccount
162+
name: ibm-namespace-scope-operator
163+
namespace: $TONS
164+
roleRef:
165+
kind: Role
166+
name: namespace-scope-client
167+
apiGroup: rbac.authorization.k8s.io
168+
EOF

0 commit comments

Comments
 (0)