at 22.05-pre 6.1 kB view raw
1# This tests Keycloak: it starts the service, creates a realm with an 2# OIDC client and a user, and simulates the user logging in to the 3# client using their Keycloak login. 4 5let 6 certs = import ./common/acme/server/snakeoil-certs.nix; 7 frontendUrl = "https://${certs.domain}/auth"; 8 initialAdminPassword = "h4IhoJFnt2iQIR9"; 9 10 keycloakTest = import ./make-test-python.nix ( 11 { pkgs, databaseType, ... }: 12 { 13 name = "keycloak"; 14 meta = with pkgs.lib.maintainers; { 15 maintainers = [ talyz ]; 16 }; 17 18 nodes = { 19 keycloak = { ... }: { 20 21 security.pki.certificateFiles = [ 22 certs.ca.cert 23 ]; 24 25 networking.extraHosts = '' 26 127.0.0.1 ${certs.domain} 27 ''; 28 29 services.keycloak = { 30 enable = true; 31 inherit frontendUrl initialAdminPassword; 32 sslCertificate = certs.${certs.domain}.cert; 33 sslCertificateKey = certs.${certs.domain}.key; 34 database = { 35 type = databaseType; 36 username = "bogus"; 37 passwordFile = pkgs.writeText "dbPassword" "wzf6vOCbPp6cqTH"; 38 }; 39 }; 40 41 environment.systemPackages = with pkgs; [ 42 xmlstarlet 43 libtidy 44 jq 45 ]; 46 }; 47 }; 48 49 testScript = 50 let 51 client = { 52 clientId = "test-client"; 53 name = "test-client"; 54 redirectUris = [ "urn:ietf:wg:oauth:2.0:oob" ]; 55 }; 56 57 user = { 58 firstName = "Chuck"; 59 lastName = "Testa"; 60 username = "chuck.testa"; 61 email = "chuck.testa@example.com"; 62 }; 63 64 password = "password1234"; 65 66 realm = { 67 enabled = true; 68 realm = "test-realm"; 69 clients = [ client ]; 70 users = [( 71 user // { 72 enabled = true; 73 credentials = [{ 74 type = "password"; 75 temporary = false; 76 value = password; 77 }]; 78 } 79 )]; 80 }; 81 82 realmDataJson = pkgs.writeText "realm-data.json" (builtins.toJSON realm); 83 84 jqCheckUserinfo = pkgs.writeText "check-userinfo.jq" '' 85 if { 86 "firstName": .given_name, 87 "lastName": .family_name, 88 "username": .preferred_username, 89 "email": .email 90 } != ${builtins.toJSON user} then 91 error("Wrong user info!") 92 else 93 empty 94 end 95 ''; 96 in '' 97 keycloak.start() 98 keycloak.wait_for_unit("keycloak.service") 99 keycloak.wait_until_succeeds("curl -sSf ${frontendUrl}") 100 101 102 ### Realm Setup ### 103 104 # Get an admin interface access token 105 keycloak.succeed( 106 "curl -sSf -d 'client_id=admin-cli' -d 'username=admin' -d 'password=${initialAdminPassword}' -d 'grant_type=password' '${frontendUrl}/realms/master/protocol/openid-connect/token' | jq -r '\"Authorization: bearer \" + .access_token' >admin_auth_header" 107 ) 108 109 # Publish the realm, including a test OIDC client and user 110 keycloak.succeed( 111 "curl -sSf -H @admin_auth_header -X POST -H 'Content-Type: application/json' -d @${realmDataJson} '${frontendUrl}/admin/realms/'" 112 ) 113 114 # Generate and save the client secret. To do this we need 115 # Keycloak's internal id for the client. 116 keycloak.succeed( 117 "curl -sSf -H @admin_auth_header '${frontendUrl}/admin/realms/${realm.realm}/clients?clientId=${client.name}' | jq -r '.[].id' >client_id", 118 "curl -sSf -H @admin_auth_header -X POST '${frontendUrl}/admin/realms/${realm.realm}/clients/'$(<client_id)'/client-secret' | jq -r .value >client_secret", 119 ) 120 121 122 ### Authentication Testing ### 123 124 # Start the login process by sending an initial request to the 125 # OIDC authentication endpoint, saving the returned page. Tidy 126 # up the HTML (XmlStarlet is picky) and extract the login form 127 # post url. 128 keycloak.succeed( 129 "curl -sSf -c cookie '${frontendUrl}/realms/${realm.realm}/protocol/openid-connect/auth?client_id=${client.name}&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=openid+email&response_type=code&response_mode=query&nonce=qw4o89g3qqm' >login_form", 130 "tidy -q -m login_form || true", 131 "xml sel -T -t -m \"_:html/_:body/_:div/_:div/_:div/_:div/_:div/_:div/_:form[@id='kc-form-login']\" -v @action login_form >form_post_url", 132 ) 133 134 # Post the login form and save the response. Once again tidy up 135 # the HTML, then extract the authorization code. 136 keycloak.succeed( 137 "curl -sSf -L -b cookie -d 'username=${user.username}' -d 'password=${password}' -d 'credentialId=' \"$(<form_post_url)\" >auth_code_html", 138 "tidy -q -m auth_code_html || true", 139 "xml sel -T -t -m \"_:html/_:body/_:div/_:div/_:div/_:div/_:div/_:input[@id='code']\" -v @value auth_code_html >auth_code", 140 ) 141 142 # Exchange the authorization code for an access token. 143 keycloak.succeed( 144 "curl -sSf -d grant_type=authorization_code -d code=$(<auth_code) -d client_id=${client.name} -d client_secret=$(<client_secret) -d redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob '${frontendUrl}/realms/${realm.realm}/protocol/openid-connect/token' | jq -r '\"Authorization: bearer \" + .access_token' >auth_header" 145 ) 146 147 # Use the access token on the OIDC userinfo endpoint and check 148 # that the returned user info matches what we initialized the 149 # realm with. 150 keycloak.succeed( 151 "curl -sSf -H @auth_header '${frontendUrl}/realms/${realm.realm}/protocol/openid-connect/userinfo' | jq -f ${jqCheckUserinfo}" 152 ) 153 ''; 154 } 155 ); 156in 157{ 158 postgres = keycloakTest { databaseType = "postgresql"; }; 159 mysql = keycloakTest { databaseType = "mysql"; }; 160}