-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #633 from yaacov/ci-create-user-reader-and-admin-r…
…oles Add CI service account for admin user and reader roles
- Loading branch information
Showing
3 changed files
with
122 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,131 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
# check if forklift-user account exist | ||
export SERVICE_ACCOUNT=forklift-user | ||
export SERVICE_ACCOUNT=forklift | ||
export NAMESPACE=default | ||
|
||
function setup_servie_account_token () { | ||
# Create forklift-user service account | ||
# Function for creating forklift roles | ||
# ------------------------------------ | ||
cat <<EOF | kubectl apply -f - | ||
function setup_k8s_roles () { | ||
|
||
cat <<EOF | kubectl apply -f - | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: forklift-reader | ||
rules: | ||
- apiGroups: ["forklift.konveyor.io"] | ||
resources: ["*"] | ||
verbs: ["get", "watch", "list"] | ||
- apiGroups: ["console.openshift.io"] | ||
resources: ["*"] | ||
verbs: ["get", "watch", "list"] | ||
EOF | ||
|
||
cat <<EOF | kubectl apply -f - | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: forklift-user | ||
rules: | ||
- apiGroups: ["forklift.konveyor.io"] | ||
resources: ["*"] | ||
verbs: ["*"] | ||
- apiGroups: ["console.openshift.io"] | ||
resources: ["*"] | ||
verbs: ["get", "watch", "list"] | ||
EOF | ||
} | ||
|
||
# Function for creating a forklift-user service account | ||
# $1 is the service account name | ||
# ------------------------------------ | ||
function setup_servie_account () { | ||
service_account=$1 | ||
|
||
cat <<EOF | kubectl apply -f - | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: ${SERVICE_ACCOUNT} | ||
name: ${service_account} | ||
namespace: ${NAMESPACE} | ||
automountServiceAccountToken: true | ||
EOF | ||
|
||
cat <<EOF | kubectl apply -f - | ||
cat <<EOF | kubectl apply -f - | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: ${SERVICE_ACCOUNT} | ||
name: ${service_account} | ||
namespace: ${NAMESPACE} | ||
annotations: | ||
kubernetes.io/service-account.name: ${SERVICE_ACCOUNT} | ||
kubernetes.io/service-account.name: ${service_account} | ||
type: kubernetes.io/service-account-token | ||
EOF | ||
} | ||
|
||
# Function for binding roles to service account | ||
# $1 is the service account name | ||
# $2 is the role | ||
# --------------------------------------------- | ||
function bind_service_accont_to_role () { | ||
service_account=$1 | ||
role=$2 | ||
|
||
# Make forklift-user an admin | ||
# --------------------------- | ||
kubectl create clusterrolebinding ${SERVICE_ACCOUNT}-forklift-user \ | ||
--clusterrole=cluster-admin \ | ||
--serviceaccount=${NAMESPACE}:${SERVICE_ACCOUNT} | ||
cat <<EOF | kubectl apply -f - | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: ${service_account} | ||
namespace: ${NAMESPACE} | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: ${role} | ||
subjects: | ||
- kind: ServiceAccount | ||
name: ${service_account} | ||
namespace: ${NAMESPACE} | ||
EOF | ||
} | ||
|
||
if kubectl get serviceaccount ${SERVICE_ACCOUNT} -n ${NAMESPACE} >/dev/null 2>&1 ; then | ||
echo "Service account ${SERVICE_ACCOUNT} already exist" | ||
else | ||
echo "Creating service account ${SERVICE_ACCOUNT}" | ||
|
||
setup_servie_account_token | ||
fi | ||
# Creare forklift user and reader roles | ||
# ------------------------------------- | ||
setup_k8s_roles | ||
|
||
echo "Creating/Updating service accounts ${SERVICE_ACCOUNT}, ${SERVICE_ACCOUNT}-user, ${SERVICE_ACCOUNT}-reader" | ||
|
||
setup_servie_account ${SERVICE_ACCOUNT}-admin | ||
bind_service_accont_to_role ${SERVICE_ACCOUNT}-admin cluster-admin | ||
|
||
setup_servie_account ${SERVICE_ACCOUNT}-user | ||
bind_service_accont_to_role ${SERVICE_ACCOUNT}-user forklift-user | ||
|
||
setup_servie_account ${SERVICE_ACCOUNT}-reader | ||
bind_service_accont_to_role ${SERVICE_ACCOUNT}-reader forklift-reader | ||
|
||
# Print out token | ||
export TOKEN=$(kubectl get secret ${SERVICE_ACCOUNT} -n ${NAMESPACE} -o=jsonpath={.data.token} | base64 -d) | ||
# Print out tokens | ||
export TOKEN_ADMIN=$(kubectl get secret ${SERVICE_ACCOUNT}-admin -n ${NAMESPACE} -o=jsonpath={.data.token} | base64 -d) | ||
export TOKEN_USER=$(kubectl get secret ${SERVICE_ACCOUNT}-user -n ${NAMESPACE} -o=jsonpath={.data.token} | base64 -d) | ||
export TOKEN_READER=$(kubectl get secret ${SERVICE_ACCOUNT}-reader -n ${NAMESPACE} -o=jsonpath={.data.token} | base64 -d) | ||
|
||
echo "Token:" | ||
echo "------" | ||
echo ${TOKEN} | ||
echo | ||
echo | ||
echo Tokens: | ||
echo "-------" | ||
echo forklift-admin: | ||
echo export TOKEN_ADMIN=${TOKEN_ADMIN} | ||
echo | ||
echo forklift-user: | ||
echo export TOKEN_USER=${TOKEN_USER} | ||
echo | ||
echo forklift-reader: | ||
echo export TOKEN_READER=${TOKEN_READER} | ||
echo | ||
echo Note: | ||
echo to use he tokens set BRIDGE_K8S_AUTH_BEARER_TOKEN | ||
echo export TOKEN_ADMIN=... | ||
echo export BRIDGE_K8S_AUTH_BEARER_TOKEN=$\{TOKEN_ADMIN \| TOKEN_USER \| TOKEN_READER\} | ||
echo | ||
echo before starting the bridge | ||
echo npm run console |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters