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

feat: sso setup #1026

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions web/kratos/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN apk add gettext
COPY . /usr/bin/app/kratos

RUN mkdir -p /etc/config/kratos && chmod a+x /usr/bin/app/kratos/entrypoint.sh && \
chmod a+x /usr/bin/app/kratos/oidc-setup.sh && \
polaroi8d marked this conversation as resolved.
Show resolved Hide resolved
chown -R 10000:10000 /etc/config/kratos && \
chmod -R 700 /etc/config/kratos

Expand Down
2 changes: 2 additions & 0 deletions web/kratos/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export OIDC_GOOGLE_SCHEMA_MAPPER
OIDC_AZURE_SCHEMA_MAPPER=$(base64 -w 0 /usr/bin/app/kratos/oidc/azure.schema.jsonnet)
export OIDC_AZURE_SCHEMA_MAPPER

source "/usr/bin/app/kratos/oidc-setup.sh" || exit 1

envsubst '${KRATOS_URL}, ${KRATOS_ADMIN_URL}, ${CRUX_UI_URL}, ${FROM_EMAIL}, ${FROM_NAME}, ${OIDC_GITLABHUB_SCHEMA_MAPPER}, ${OIDC_GOOGLE_SCHEMA_MAPPER}, ${OIDC_AZURE_SCHEMA_MAPPER}' < /usr/bin/app/kratos/kratos.template.yaml > /etc/config/kratos/kratos.yaml

exec kratos $@
48 changes: 0 additions & 48 deletions web/kratos/kratos.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,6 @@ selfservice:
enabled: false
config:
base_redirect_uri: ${KRATOS_URL}
providers:
- id: gitlab
provider: gitlab
mapper_url: "base64://${OIDC_GITLABHUB_SCHEMA_MAPPER}"
auth_url: https://gitlab.com/oauth/authorize
token_url: https://gitlab.com/oauth/token
issuer_url: https://gitlab.com
client_id: ""
client_secret: ""
scope:
- read_user
- openid
- email
- profile
- id: github
provider: github
mapper_url: "base64://${OIDC_GITLABHUB_SCHEMA_MAPPER}"
auth_url: https://github.com/login/oauth/authorize
token_url: https://github.com/login/oauth/access_token
issuer_url: https://github.com
client_id: ""
client_secret: ""
scope:
- read:user
- user:email
- id: google
provider: google
mapper_url: "base64://${OIDC_GOOGLE_SCHEMA_MAPPER}"
auth_url: https://accounts.google.com/o/oauth2/v2/auth
token_url: https://www.googleapis.com/oauth2/v4/token
issuer_url: https://accounts.google.com
client_id: ""
client_secret: ""
scope:
- openid
- email
- profile
- id: azure
provider: microsoft
mapper_url: "base64://${OIDC_AZURE_SCHEMA_MAPPER}"
client_id: ""
client_secret: ""
microsoft_tenant: common
subject_source: userinfo
scope:
- openid
- email
- profile
code:
enabled: true
flows:
Expand Down
71 changes: 71 additions & 0 deletions web/kratos/oidc-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env sh
polaroi8d marked this conversation as resolved.
Show resolved Hide resolved
set +eu

# Define templates for each provider, make sure to not resolve envs here as not all of them might be needed
TEMPLATES_GITLAB="{\"id\":\"gitlab\",\"provider\":\"gitlab\",\"mapper_url\":\"base64://\${OIDC_GITLABHUB_SCHEMA_MAPPER}\",\"client_id\":\"\${TEMPLATE_CLIENT_ID}\",\"client_secret\":\"\${TEMPLATE_CLIENT_SECRET}\",\"scope\":[\"read_user\",\"openid\",\"email\",\"profile\"],\"auth_url\":\"https://gitlab.com/oauth/authorize\",\"token_url\":\"https://gitlab.com/oauth/token\",\"issuer_url\":\"https://gitlab.com\"}"
TEMPLATES_GITHUB="{\"id\":\"github\",\"provider\":\"github\",\"mapper_url\":\"base64://\${OIDC_GITLABHUB_SCHEMA_MAPPER}\",\"client_id\":\"\${TEMPLATE_CLIENT_ID}\",\"client_secret\":\"\${TEMPLATE_CLIENT_SECRET}\",\"scope\":[\"read:user\",\"user:email\"],\"auth_url\":\"https://github.com/login/oauth/authorize\",\"token_url\":\"https://github.com/login/oauth/access_token\",\"issuer_url\":\"https://github.com\"}"
TEMPLATES_GOOGLE="{\"id\":\"google\",\"provider\":\"google\",\"mapper_url\":\"base64://\${OIDC_GOOGLE_SCHEMA_MAPPER}\",\"client_id\":\"\${TEMPLATE_CLIENT_ID}\",\"client_secret\":\"\${TEMPLATE_CLIENT_SECRET}\",\"scope\":[\"openid\",\"email\",\"profile\"],\"auth_url\":\"https://accounts.google.com/o/oauth2/v2/auth\",\"token_url\":\"https://www.googleapis.com/oauth2/v4/token\",\"issuer_url\":\"https://accounts.google.com\"}"
TEMPLATES_AZURE="{\"id\":\"azure\",\"provider\":\"microsoft\",\"mapper_url\":\"base64://\${OIDC_AZURE_SCHEMA_MAPPER}\",\"client_id\":\"\${TEMPLATE_CLIENT_ID}\",\"client_secret\":\"\${TEMPLATE_CLIENT_SECRET}\",\"scope\":[\"openid\",\"email\",\"profile\"],\"microsoft_tenant\":\"common\",\"subject_source\":\"userinfo\"}"

# Checks the given provider environment variables and appends the JSON object to the PROVIDERS variable
# based on the template defined above.
# Arguments: $1 = upper case provider name, $2 = mapper environment variable name
# ClientID env is "OIDC_[provider]_CLIENT_ID", secret env is "OIDC_[provider]_CLIENT_SECRET"
checkProvider() {
envClientId="OIDC_$1_CLIENT_ID"
envClientSecret="OIDC_$1_CLIENT_SECRET"

set +eu # Disable unset variables check as client ID and secret might be unset
[ -z "$(eval echo \$$envClientId)" ]
clientIdSet=$?

[ -z "$(eval echo \$$envClientSecret)" ]
clientSecretSet=$?
set -eu

if [ "$clientIdSet" -ne "$clientSecretSet" ]; then
echo "$envClientId or $envClientSecret not set"
exit 1
fi

if [ "$clientIdSet" = 1 ] && [ "$clientSecretSet" = 1 ]; then
set +eu
mapperValue=$(eval echo \$$2)

if [ -z "$mapperValue" ]; then
echo "$2 missing"
exit 1
fi
set -eu

clientId="$(eval echo \$$envClientId)"
clientSecret="$(eval echo \$$envClientSecret)"

templateString=$(eval echo \$TEMPLATES_$1)

newTemplateString=$(echo $templateString | sed -e "s/\${TEMPLATE_CLIENT_ID}/$clientId/g")
newTemplateString=$(echo $newTemplateString | sed -e "s/\${TEMPLATE_CLIENT_SECRET}/$clientSecret/g")
newTemplateString=$(echo $newTemplateString | sed -e "s/\${$2}/$mapperValue/g")

PROVIDERS="${PROVIDERS}$newTemplateString,"

echo "$1 OIDC client setup"
fi
}

PROVIDERS="["

checkProvider "GITLAB" "OIDC_GITLABHUB_SCHEMA_MAPPER"
checkProvider "GITHUB" "OIDC_GITLABHUB_SCHEMA_MAPPER"
checkProvider "GOOGLE" "OIDC_GOOGLE_SCHEMA_MAPPER"
checkProvider "AZURE" "OIDC_AZURE_SCHEMA_MAPPER"

# Strip ',' at the end of PROVIDERS
if [ "${#PROVIDERS}" -gt 1 ]; then
PROVIDERS="${PROVIDERS::-1}"
export SELFSERVICE_METHODS_OIDC_ENABLED=true
else
unset SELFSERVICE_METHODS_OIDC_ENABLED
fi

export SELFSERVICE_METHODS_OIDC_CONFIG_PROVIDERS="${PROVIDERS}]"
Loading