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 6 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
8 changes: 8 additions & 0 deletions .ci/cico_updates_openshift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@ 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}
provisionOAuth
startNewWorkspace
waitWorkspaceStart
}

init
overrideDefaults
provisionOpenShiftOAuthUser
initStableTemplates "openshift" "stable"
runTests
5 changes: 4 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,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 @@ -35,12 +36,14 @@ runTests() {
# Deploy Eclipse Che applying CR
applyOlmCR
waitEclipseCheDeployed "nightly"
provisionOAuth
startNewWorkspace
waitWorkspaceStart
}

init
overrideDefaults
provisionOpenShiftOAuthUser
patchEclipseCheOperatorSubscription
printOlmCheObjects
runTests
3 changes: 3 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 @@ -36,12 +37,14 @@ runTests() {
# Deploy Eclipse Che applying CR
applyOlmCR
waitEclipseCheDeployed "nightly"
provisionOAuth
startNewWorkspace
waitWorkspaceStart
}

init
overrideDefaults
provisionOpenShiftOAuthUser
patchEclipseCheOperatorSubscription
printOlmCheObjects
runTests
25 changes: 20 additions & 5 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 All @@ -35,7 +33,7 @@ init() {
export OPERATOR_IMAGE="quay.io/eclipse/che-operator:test"
export DEFAULT_DEVFILE="https://raw.githubusercontent.com/eclipse/che-devfile-registry/master/devfiles/quarkus/devfile.yaml"
export CHE_EXPOSURE_STRATEGY="multi-host"
export OAUTH="false"
export OAUTH="true"

export XDG_DATA_HOME=/tmp/xdg_data
export XDG_CACHE_HOME=/tmp/xdg_cache
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 provisionOpenShiftOAuthUser() {
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
}
95 changes: 95 additions & 0 deletions .github/bin/oauth-provision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/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

# Link ocp account with Keycloak IDP
function provisionOAuth() {
OCP_USER_UID=$(oc get user user -o=jsonpath='{.metadata.uid}')

IDP_USER="admin"
# Get Eclipse Che IDP secrets and decode to use to connect to IDP
IDP_PASSWORD=$(oc get secret che-identity-secret -n eclipse-che -o=jsonpath='{.data.password}' | base64 --decode)

# Get Auth Route
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

# Get the oauth client from Eclipse Che Custom Resource
OAUTH_CLIENT_NAME=$(oc get checluster eclipse-che -n eclipse-che -o=jsonpath='{.spec.auth.oAuthClientName}')

# Obtain from Keycloak the token to make api request authentication
IDP_TOKEN=$(curl -k --location --request POST ''$IDP_HOST'/auth/realms/master/protocol/openid-connect/token' \
--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] IDP Token: $IDP_TOKEN"

# Get admin user id from IDP
CHE_USER_ID=$(curl --location -k --request GET ''$IDP_HOST'/auth/admin/realms/che/users' \
--header 'Authorization: Bearer '$IDP_TOKEN'' | jq -r '.[] | select(.username == "admin").id' )
tolusha marked this conversation as resolved.
Show resolved Hide resolved

echo -e "[INFO] Eclipse CHE user ID: $CHE_USER_ID"

# Request to link Openshift user with Identity Provider user. In this case we are linked an existed user in IDP
curl --location -k --request POST ''$IDP_HOST'/auth/admin/realms/che/users/'$CHE_USER_ID'/federated-identity/openshift-v4' \
--header 'Authorization: Bearer '$IDP_TOKEN'' \
--header 'Content-Type: application/json' \
--data '{
"identityProvider": "openshift-v4",
"userId": "'$OCP_USER_UID'",
"userName": "admin"
}'

# Create OAuthClientAuthorization object for Eclipse Che in Cluster.
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
)
# Create SQL script
echo -e "Created authorization client: $OAUTHCLIENTAuthorization"
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

# Insert sql script inside of postgres and execute it.
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=