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

Add oauth to tests #670

Merged
merged 7 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
9 changes: 9 additions & 0 deletions .ci/cico_updates_openshift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ set -x

export OPERATOR_REPO=$(dirname $(dirname $(readlink -f "$0")));
source "${OPERATOR_REPO}"/.github/bin/common.sh
source "${OPERATOR_REPO}"/.github/bin/oauth-provision.sh

#Stop execution on any error
trap "catchFinish" EXIT SIGINT

overrideDefaults() {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
export OAUTH="true"
flacatus marked this conversation as resolved.
Show resolved Hide resolved
}

runTests() {
"${OPERATOR_REPO}"/olm/testUpdate.sh "openshift" "stable" ${NAMESPACE}
waitEclipseCheDeployed ${LAST_PACKAGE_VERSION}
oauthProvisioned
provisionPostgres
flacatus marked this conversation as resolved.
Show resolved Hide resolved
startNewWorkspace
waitWorkspaceStart
}

init
overrideDefaults
provisionOpenshiftUsers
initStableTemplates "openshift" "stable"
runTests
7 changes: 6 additions & 1 deletion .ci/oci-nightly-olm.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#
# Copyright (c) 2012-2020 Red Hat, Inc.
# Copyright (c) 2012-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -22,24 +22,29 @@ set -u

export OPERATOR_REPO=$(dirname $(dirname $(readlink -f "$0")));
source "${OPERATOR_REPO}"/.github/bin/common.sh
source "${OPERATOR_REPO}"/.github/bin/oauth-provision.sh

#Stop execution on any error
trap "catchFinish" EXIT SIGINT

overrideDefaults() {
# CI_CHE_OPERATOR_IMAGE it is che operator image builded in openshift CI job workflow. More info about how works image dependencies in ci:https://github.com/openshift/ci-tools/blob/master/TEMPLATES.md#parameters-available-to-templates
export OPERATOR_IMAGE=${CI_CHE_OPERATOR_IMAGE:-"quay.io/eclipse/che-operator:nightly"}
export OAUTH="true"
}

runTests() {
# Deploy Eclipse Che applying CR
applyOlmCR
waitEclipseCheDeployed "nightly"
oauthProvisioned
provisionPostgres
startNewWorkspace
waitWorkspaceStart
}

init
provisionOpenshiftUsers
overrideDefaults
patchEclipseCheOperatorSubscription
printOlmCheObjects
Expand Down
5 changes: 5 additions & 0 deletions .ci/oci-single-host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set -u

export OPERATOR_REPO=$(dirname $(dirname $(readlink -f "$0")));
source "${OPERATOR_REPO}"/.github/bin/common.sh
source "${OPERATOR_REPO}"/.github/bin/oauth-provision.sh

#Stop execution on any error
trap "catchFinish" EXIT SIGINT
Expand All @@ -30,18 +31,22 @@ overrideDefaults() {
# CI_CHE_OPERATOR_IMAGE it is che operator image builded in openshift CI job workflow. More info about how works image dependencies in ci:https://github.com/openshift/ci-tools/blob/master/TEMPLATES.md#parameters-available-to-templates
export OPERATOR_IMAGE=${CI_CHE_OPERATOR_IMAGE:-"quay.io/eclipse/che-operator:nightly"}
export CHE_EXPOSURE_STRATEGY="single-host"
export OAUTH="true"
}

runTests() {
# Deploy Eclipse Che applying CR
applyOlmCR
waitEclipseCheDeployed "nightly"
oauthProvisioned
provisionPostgres
startNewWorkspace
waitWorkspaceStart
}

init
overrideDefaults
provisionOpenshiftUsers
patchEclipseCheOperatorSubscription
printOlmCheObjects
runTests
23 changes: 19 additions & 4 deletions .github/bin/common.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/usr/bin/env bash
#!/bin/bash
#
# Copyright (c) 2020 Red Hat, Inc.
# Copyright (c) 2012-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation

set -e
set -x
Expand Down Expand Up @@ -309,3 +307,20 @@ applyOlmCR() {
echo -e "$CR"
echo "$CR" | oc apply -n "${NAMESPACE}" -f -
}

# Create admin user inside of openshift cluster and login
function provisionOpenshiftUsers() {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
oc create secret generic htpass-secret --from-file=htpasswd="${OPERATOR_REPO}"/.github/bin/resources/users.htpasswd -n openshift-config
oc apply -f "${OPERATOR_REPO}"/.github/bin/resources/htpasswdProvider.yaml
oc adm policy add-cluster-role-to-user cluster-admin user
flacatus marked this conversation as resolved.
Show resolved Hide resolved

echo -e "[INFO] Waiting for htpasswd auth to be working up to 5 minutes"
CURRENT_TIME=$(date +%s)
ENDTIME=$(($CURRENT_TIME + 300))
while [ $(date +%s) -lt $ENDTIME ]; do
if oc login -u user -p user --insecure-skip-tls-verify=false; then
break
fi
sleep 10
done
}
94 changes: 94 additions & 0 deletions .github/bin/oauth-provision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash
#
# Copyright (c) 2012-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#

# exit immediately when a command fails
set -e
# only exit with zero if all commands of the pipeline exit successfully
set -o pipefail
# error on unset variables
set -u

export CHE_EXPOSURE_STRATEGY="single-host"
flacatus marked this conversation as resolved.
Show resolved Hide resolved

# Link ocp account with Keycloak IDP
function oauthProvisioned() {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
OCP_USER_UID=$(oc get user user -o=jsonpath='{.metadata.uid}')

IDP_USER="admin"
IDP_PASSWORD=$(oc get secret che-identity-secret -n eclipse-che -o=jsonpath='{.data.password}' | base64 --decode)

if [[ "${CHE_EXPOSURE_STRATEGY}" == "single-host" ]]; then
IDP_HOST="https://"$(oc get route che -n eclipse-che -o=jsonpath='{.spec.host}')
fi

if [[ "${CHE_EXPOSURE_STRATEGY}" == "multi-host" ]]; then
IDP_HOST="https://"$(oc get route keycloak -n eclipse-che -o=jsonpath='{.spec.host}')
fi

OAUTH_CLIENT_NAME=$(oc get checluster eclipse-che -n eclipse-che -o=jsonpath='{.spec.auth.oAuthClientName}')

TOKEN_RESULT=$(curl -k --location --request POST ''$IDP_HOST'/auth/realms/master/protocol/openid-connect/token' \
flacatus marked this conversation as resolved.
Show resolved Hide resolved
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=admin' \
flacatus marked this conversation as resolved.
Show resolved Hide resolved
--data-urlencode 'password='$IDP_PASSWORD'' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id=admin-cli' | jq -r .access_token)

echo -e "[INFO] Token: $TOKEN_RESULT"
flacatus marked this conversation as resolved.
Show resolved Hide resolved

USER_ID=$(curl --location -k --request GET ''$IDP_HOST'/auth/admin/realms/che/users' \
flacatus marked this conversation as resolved.
Show resolved Hide resolved
--header 'Authorization: Bearer '$TOKEN_RESULT'' | jq -r '.[] | select(.username == "admin").id' )

echo -e "[INFO] user id: $USER_ID"
flacatus marked this conversation as resolved.
Show resolved Hide resolved

curl --location -k --request POST ''$IDP_HOST'/auth/admin/realms/che/users/'$USER_ID'/federated-identity/openshift-v4' \
--header 'Authorization: Bearer '$TOKEN_RESULT'' \
--header 'Content-Type: application/json' \
--data '{
"identityProvider": "openshift-v4",
"userId": "'$OCP_USER_UID'",
"userName": "admin"
}'

OAUTHCLIENTAuthorization=$(
oc create -f - -o jsonpath='{.metadata.name}' <<EOF
apiVersion: oauth.openshift.io/v1
kind: OAuthClientAuthorization
metadata:
generateName: $IDP_USER:$OAUTH_CLIENT_NAME
namespace: eclipse-che
clientName: $OAUTH_CLIENT_NAME
userName: $IDP_USER
userUID: $OCP_USER_UID
scopes:
- 'user:full'
EOF
)

echo -e "Created authorization client: $OAUTHCLIENTAuthorization"
}

# Insert in Keycloak Database openshift token after linking ocp user with IDP user
function provisionPostgres() {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
cat << 'EOF' > path.sql
UPDATE federated_identity SET token ='{"access_token":"INSERT_TOKEN_HERE","expires_in":86400,"scope":"user:full","token_type":"Bearer"}'
WHERE federated_username = 'admin'
tolusha marked this conversation as resolved.
Show resolved Hide resolved
EOF

TOKEN=$(oc whoami -t)
sed -i "s|INSERT_TOKEN_HERE|$TOKEN|g" path.sql

POSTGRES_POD=$(oc get pods -o json -n eclipse-che | jq -r '.items[] | select(.metadata.name | test("postgres-")).metadata.name')

oc cp path.sql "${POSTGRES_POD}":/tmp/ -n eclipse-che
oc exec -it "${POSTGRES_POD}" -n eclipse-che -- bash -c "psql -U postgres -d keycloak -d keycloak -f /tmp/path.sql"

rm path.sql
}
12 changes: 12 additions & 0 deletions .github/bin/resources/htpasswdProvider.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
name: cluster
spec:
identityProviders:
- name: htpasswd
mappingMethod: claim
type: HTPasswd
htpasswd:
fileData:
name: htpass-secret
1 change: 1 addition & 0 deletions .github/bin/resources/users.htpasswd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user:{SHA}Et6pb+wgWTVmq3VpLJlJWWgzrck=