diff --git a/.gitignore b/.gitignore index 6370798..5e32110 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ __pycache__/ *.pyc +keycloak/*.env +keycloak/.env diff --git a/docker-compose.aimaq.override.yml b/docker-compose.aimaq.override.yml index 17fcb28..2fad1d1 100644 --- a/docker-compose.aimaq.override.yml +++ b/docker-compose.aimaq.override.yml @@ -88,3 +88,50 @@ services: depends_on: postgres: condition: service_healthy + keycloak: + image: quay.io/keycloak/keycloak:26.8.0 + restart: unless-stopped + command: ["start", "--import-realm"] + env_file: + - ./keycloak/.env + environment: + KC_DB: postgres + KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak + KC_DB_USERNAME: keycloak + KC_DB_POOL_MAX_SIZE: "10" + KC_HOSTNAME: https://aimaq.konturai.kz/auth + KC_HTTP_ENABLED: "true" + KC_HTTP_RELATIVE_PATH: /auth + KC_PROXY_HEADERS: xforwarded + KC_HEALTH_ENABLED: "true" + KC_CACHE: local + KC_LOG_LEVEL: INFO + JAVA_OPTS_KC_HEAP: "-XX:MaxRAMPercentage=65 -XX:InitialRAMPercentage=30" + volumes: + - ./keycloak/callcenter-realm.json:/opt/keycloak/data/import/callcenter-realm.json:ro + mem_limit: 900m + healthcheck: + test: ["CMD-SHELL", "exec 3<>/dev/tcp/127.0.0.1/9000 && printf 'GET /auth/health/ready HTTP/1.1\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n' >&3 && grep -q UP <&3"] + interval: 15s + timeout: 5s + retries: 20 + start_period: 90s + depends_on: + postgres: + condition: service_healthy + networks: + - default + - common_network + auth-service: + env_file: + - ./.env.production + - ./keycloak/auth-client.env + environment: + OIDC_ENABLED: "1" + OIDC_PROVIDER: keycloak + OIDC_ISSUER_URL: https://aimaq.konturai.kz/auth/realms/callcenter + OIDC_CLIENT_ID: callcenter-auth + OIDC_REDIRECT_URI: https://aimaq.konturai.kz/proxy/auth/auth/oidc/callback + OIDC_SCOPES: openid profile email + OIDC_ROLE_CLAIM: roles + OIDC_ROLE_MAP_JSON: '{"admin":"admin","supervisor":"supervisor","operator":"operator","analyst":"analyst"}' diff --git a/keycloak/callcenter-realm.json b/keycloak/callcenter-realm.json new file mode 100644 index 0000000..d006c1b --- /dev/null +++ b/keycloak/callcenter-realm.json @@ -0,0 +1,326 @@ +{ + "realm": "callcenter", + "displayName": "Аймак Колл-центр", + "enabled": true, + "sslRequired": "external", + "registrationAllowed": false, + "loginWithEmailAllowed": false, + "duplicateEmailsAllowed": true, + "resetPasswordAllowed": false, + "editUsernameAllowed": false, + "rememberMe": true, + "bruteForceProtected": true, + "permanentLockout": false, + "failureFactor": 10, + "waitIncrementSeconds": 60, + "maxFailureWaitSeconds": 900, + "internationalizationEnabled": true, + "supportedLocales": [ + "ru", + "kk", + "en" + ], + "defaultLocale": "ru", + "accessTokenLifespan": 900, + "ssoSessionIdleTimeout": 43200, + "ssoSessionMaxLifespan": 86400, + "passwordPolicy": "length(12) and notUsername", + "roles": { + "realm": [ + { + "name": "admin", + "description": "Администратор" + }, + { + "name": "supervisor", + "description": "Супервизор" + }, + { + "name": "operator", + "description": "Оператор" + }, + { + "name": "analyst", + "description": "Аналитик" + } + ] + }, + "clients": [ + { + "clientId": "callcenter-platform", + "name": "Contact-center platform API", + "enabled": true, + "bearerOnly": true, + "publicClient": false, + "standardFlowEnabled": false, + "directAccessGrantsEnabled": false + }, + { + "clientId": "callcenter-auth", + "name": "Contact-center login bridge", + "enabled": true, + "publicClient": false, + "clientAuthenticatorType": "client-secret", + "secret": "${AUTH_CLIENT_SECRET}", + "standardFlowEnabled": true, + "directAccessGrantsEnabled": false, + "implicitFlowEnabled": false, + "serviceAccountsEnabled": false, + "rootUrl": "https://aimaq.konturai.kz", + "redirectUris": [ + "https://aimaq.konturai.kz/proxy/auth/auth/oidc/callback" + ], + "webOrigins": [ + "https://aimaq.konturai.kz" + ], + "attributes": { + "pkce.code.challenge.method": "S256", + "post.logout.redirect.uris": "https://aimaq.konturai.kz/*" + }, + "defaultClientScopes": [ + "basic", + "profile", + "email", + "roles", + "web-origins", + "acr" + ], + "optionalClientScopes": [], + "protocolMappers": [ + { + "name": "tenant_id", + "protocol": "openid-connect", + "protocolMapper": "oidc-usermodel-attribute-mapper", + "config": { + "user.attribute": "tenant_id", + "claim.name": "tenant_id", + "jsonType.label": "String", + "access.token.claim": "true", + "id.token.claim": "true", + "userinfo.token.claim": "true" + } + }, + { + "name": "roles", + "protocol": "openid-connect", + "protocolMapper": "oidc-usermodel-realm-role-mapper", + "config": { + "claim.name": "roles", + "multivalued": "true", + "jsonType.label": "String", + "access.token.claim": "true", + "id.token.claim": "true", + "userinfo.token.claim": "true" + } + }, + { + "name": "platform-audience", + "protocol": "openid-connect", + "protocolMapper": "oidc-audience-mapper", + "config": { + "included.client.audience": "callcenter-platform", + "access.token.claim": "true", + "id.token.claim": "false" + } + } + ] + }, + { + "clientId": "callcenter-web", + "name": "Contact-center web", + "enabled": true, + "publicClient": true, + "standardFlowEnabled": true, + "directAccessGrantsEnabled": false, + "implicitFlowEnabled": false, + "rootUrl": "https://aimaq.konturai.kz", + "redirectUris": [ + "https://aimaq.konturai.kz/*" + ], + "webOrigins": [ + "https://aimaq.konturai.kz" + ], + "attributes": { + "pkce.code.challenge.method": "S256", + "post.logout.redirect.uris": "https://aimaq.konturai.kz/*" + }, + "defaultClientScopes": [ + "basic", + "profile", + "email", + "roles", + "web-origins", + "acr" + ], + "optionalClientScopes": [], + "protocolMappers": [ + { + "name": "tenant_id", + "protocol": "openid-connect", + "protocolMapper": "oidc-usermodel-attribute-mapper", + "config": { + "user.attribute": "tenant_id", + "claim.name": "tenant_id", + "jsonType.label": "String", + "access.token.claim": "true", + "id.token.claim": "true", + "userinfo.token.claim": "true" + } + }, + { + "name": "roles", + "protocol": "openid-connect", + "protocolMapper": "oidc-usermodel-realm-role-mapper", + "config": { + "claim.name": "roles", + "multivalued": "true", + "jsonType.label": "String", + "access.token.claim": "true", + "id.token.claim": "true", + "userinfo.token.claim": "true" + } + }, + { + "name": "platform-audience", + "protocol": "openid-connect", + "protocolMapper": "oidc-audience-mapper", + "config": { + "included.client.audience": "callcenter-platform", + "access.token.claim": "true", + "id.token.claim": "false" + } + } + ] + }, + { + "clientId": "callcenter-voice", + "name": "Voice engine service account", + "enabled": true, + "publicClient": false, + "clientAuthenticatorType": "client-secret", + "secret": "${VOICE_CLIENT_SECRET}", + "standardFlowEnabled": false, + "directAccessGrantsEnabled": false, + "serviceAccountsEnabled": true, + "defaultClientScopes": [ + "basic", + "roles" + ], + "protocolMappers": [ + { + "name": "platform-audience", + "protocol": "openid-connect", + "protocolMapper": "oidc-audience-mapper", + "config": { + "included.client.audience": "callcenter-platform", + "access.token.claim": "true", + "id.token.claim": "false" + } + } + ] + } + ], + "users": [ + { + "id": "usr_bdcbef7c68", + "username": "admin", + "firstName": "System", + "lastName": "Admin", + "enabled": true, + "attributes": { + "tenant_id": [ + "dev-aimaq" + ] + }, + "realmRoles": [ + "admin" + ], + "credentials": [ + { + "type": "password", + "value": "${KC_PASSWORD_ADMIN}", + "temporary": false + } + ] + }, + { + "id": "usr_8164b6518f", + "username": "supervisor", + "firstName": "Shift", + "lastName": "Supervisor", + "enabled": true, + "attributes": { + "tenant_id": [ + "dev-aimaq" + ] + }, + "realmRoles": [ + "supervisor" + ], + "credentials": [ + { + "type": "password", + "value": "${KC_PASSWORD_SUPERVISOR}", + "temporary": false + } + ] + }, + { + "id": "usr_add9799554", + "username": "operator", + "firstName": "Call", + "lastName": "Operator", + "enabled": true, + "attributes": { + "tenant_id": [ + "dev-aimaq" + ] + }, + "realmRoles": [ + "operator" + ], + "credentials": [ + { + "type": "password", + "value": "${KC_PASSWORD_OPERATOR}", + "temporary": false + } + ] + }, + { + "id": "usr_f342af6751", + "username": "analyst", + "firstName": "Business", + "lastName": "Analyst", + "enabled": true, + "attributes": { + "tenant_id": [ + "dev-aimaq" + ] + }, + "realmRoles": [ + "analyst" + ], + "credentials": [ + { + "type": "password", + "value": "${KC_PASSWORD_ANALYST}", + "temporary": false + } + ] + } + ], + "components": { + "org.keycloak.userprofile.UserProfileProvider": [ + { + "providerId": "declarative-user-profile", + "subComponents": {}, + "config": { + "kc.user.profile.config": [ + "{\"attributes\": [{\"name\": \"username\", \"displayName\": \"Логин\", \"validations\": {\"length\": {\"min\": 3, \"max\": 255}, \"username-prohibited-characters\": {}, \"up-username-not-idn-homograph\": {}}, \"permissions\": {\"view\": [\"admin\", \"user\"], \"edit\": [\"admin\", \"user\"]}, \"multivalued\": false}, {\"name\": \"email\", \"displayName\": \"Email\", \"validations\": {\"email\": {}, \"length\": {\"max\": 255}}, \"permissions\": {\"view\": [\"admin\", \"user\"], \"edit\": [\"admin\", \"user\"]}, \"multivalued\": false}, {\"name\": \"firstName\", \"displayName\": \"Имя\", \"validations\": {\"length\": {\"max\": 255}, \"person-name-prohibited-characters\": {}}, \"permissions\": {\"view\": [\"admin\", \"user\"], \"edit\": [\"admin\", \"user\"]}, \"multivalued\": false}, {\"name\": \"lastName\", \"displayName\": \"Фамилия\", \"validations\": {\"length\": {\"max\": 255}, \"person-name-prohibited-characters\": {}}, \"permissions\": {\"view\": [\"admin\", \"user\"], \"edit\": [\"admin\", \"user\"]}, \"multivalued\": false}, {\"name\": \"tenant_id\", \"displayName\": \"Арендатор\", \"validations\": {\"length\": {\"min\": 1, \"max\": 64}}, \"permissions\": {\"view\": [\"admin\", \"user\"], \"edit\": [\"admin\"]}, \"multivalued\": false}], \"groups\": [{\"name\": \"user-metadata\", \"displayHeader\": \"User metadata\", \"displayDescription\": \"Attributes, which refer to user metadata\"}]}" + ] + } + } + ] + } +}