|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +export PATH=/opt/keycloak/bin:$PATH |
| 21 | + |
| 22 | +kcadm.sh config credentials --server http://127.0.0.1:8080 --realm master --user admin --password admin |
| 23 | + |
| 24 | +# create realm |
| 25 | +kcadm.sh create realms -s realm=basic -s enabled=true |
| 26 | + |
| 27 | +# set realm keys with specific private key, reuse tls cert and key |
| 28 | +PRIVATE_KEY=$(awk 'NF {sub(/\r/, ""); printf "%s\\n", $0}' /opt/keycloak/conf/server.key.pem) |
| 29 | +CERTIFICATE=$(awk 'NF {sub(/\r/, ""); printf "%s\\n", $0}' /opt/keycloak/conf/server.crt.pem) |
| 30 | +kcadm.sh create components -r basic -s name=rsa-apisix -s providerId=rsa \ |
| 31 | + -s providerType=org.keycloak.keys.KeyProvider \ |
| 32 | + -s 'config.priority=["1000"]' \ |
| 33 | + -s 'config.enabled=["true"]' \ |
| 34 | + -s 'config.active=["true"]' \ |
| 35 | + -s "config.privateKey=[\"$PRIVATE_KEY\"]" \ |
| 36 | + -s "config.certificate=[\"$CERTIFICATE\"]" \ |
| 37 | + -s 'config.algorithm=["RS256"]' |
| 38 | + |
| 39 | +# create client apisix |
| 40 | +kcadm.sh create clients \ |
| 41 | + -r basic \ |
| 42 | + -s clientId=apisix \ |
| 43 | + -s enabled=true \ |
| 44 | + -s clientAuthenticatorType=client-secret \ |
| 45 | + -s secret=secret \ |
| 46 | + -s 'redirectUris=["*"]' \ |
| 47 | + -s 'directAccessGrantsEnabled=true' |
| 48 | + |
| 49 | +# add audience to client apisix, so that the access token will contain the client id ("apisix") as audience |
| 50 | +APISIX_CLIENT_UUID=$(kcadm.sh get clients -r basic -q clientId=apisix | jq -r '.[0].id') |
| 51 | +kcadm.sh create clients/$APISIX_CLIENT_UUID/protocol-mappers/models \ |
| 52 | + -r basic \ |
| 53 | + -s protocol=openid-connect \ |
| 54 | + -s name=aud \ |
| 55 | + -s protocolMapper=oidc-audience-mapper \ |
| 56 | + -s 'config."id.token.claim"=false' \ |
| 57 | + -s 'config."access.token.claim"=true' \ |
| 58 | + -s 'config."included.client.audience"=apisix' |
| 59 | + |
| 60 | +# create client apisix |
| 61 | +kcadm.sh create clients \ |
| 62 | + -r basic \ |
| 63 | + -s clientId=apisix \ |
| 64 | + -s enabled=true \ |
| 65 | + -s clientAuthenticatorType=client-secret \ |
| 66 | + -s secret=secret \ |
| 67 | + -s 'redirectUris=["*"]' \ |
| 68 | + -s 'directAccessGrantsEnabled=true' |
| 69 | + |
| 70 | +# create client apisix-no-aud, without client id audience |
| 71 | +# according to Keycloak's default implementation, when unconfigured, |
| 72 | +# only the account is listed as an audience, not the client id |
| 73 | + |
| 74 | +kcadm.sh create clients \ |
| 75 | + -r basic \ |
| 76 | + -s clientId=apisix-no-aud \ |
| 77 | + -s enabled=true \ |
| 78 | + -s clientAuthenticatorType=client-secret \ |
| 79 | + -s secret=secret \ |
| 80 | + -s 'redirectUris=["*"]' \ |
| 81 | + -s 'directAccessGrantsEnabled=true' |
| 82 | + |
| 83 | +# create user jack |
| 84 | +kcadm.sh create users -r basic -s username=jack -s enabled=true |
| 85 | +kcadm.sh set-password -r basic --username jack --new-password jack |
0 commit comments