Operations/Keycloak

[Keycloak] Grafana Keycloak 연동

혬수 2025. 6. 15. 15:04

 

이전 게시글에서 Keycloak에 대해 배워봤으니, 

Grafana 대시보드 진입시, Keycloak을 통해 접근하는 방식을 배워보도록 하자!

 

realm, role 등 Keycloak 관련 용어들 모르면 아래 게시글 참고!

https://dev-shim99.tistory.com/64

 

[Keycloak] Keycloak 개요

📌 Keycloak이란?오픈소스 ID & Access Management 솔루션OIDC 및 OAuth 2.0, SAML 2.0을 지원하여 많은 시스템에 SSO 및 중앙집중식 사용자 관리를 제공 관련 용어RealmKeycloak의 관리 영역 단위로, 사용자, 클

dev-shim99.tistory.com

 

 

일반적으로 같은 조직·유저 그룹을 공유하는 애플리케이션 단일 Realm에서 여러 Client로 관리하고,

완전 별도 조직이나 고객(테넌트)용이라면 애플리케이션별(또는 테넌트별) Realm 분리를 고려

 

필자의 경우, 같은 조직에서 여러 애플리케이션을 관리하는 것 고려하여, 단일 Realm에서 Client를 생성해서 진행하도록 하겠다!

기본적으로 master realm은 존재하므로 1번과 2번 과정은 생략했다.

 

📌 1. Realm 생성 (선택)

Realm이란 Keycloak의 관리 영역 단위로, 사용자, 클라이언트, 역할, 그룹 등을 격리하여 운영한다.

 

📌 2. Realm 설정 (선택)

 

📌 3. Client 생성

Client란 Keycloak에 연결된 애플리케이션 또는 서비스로,

OIDC나 SAML 프로토콜을 통해 Keycloak과 통신하며, 리디렉트 URI, 권한 부여 타입(confidential/public) 등을 설정한다.

 

여기서는 Client가 Grafana가 되겠다.

 

 

  • Client type: OpenID Connect
  • Client authentication: On
  • Authentication flow
    • Standard flow
    • Direct access grants
  • Root URL , Home URL, Valid post logout redirect URIs, Web origins: https://<grafana-domain>
  • Valid redirect URIs : https://<grafana-domain>/login/generic_oauth

📌 4. Client  Role 생성

Grafana에서 사용될 Role을 생성한다.

 

  1. 생성한 [Client] 클릭 > 상단 메뉴 중 [Role] 클릭
  2. admin, editor, viewer 생성

 

📌 5. Group 생성 (선택)

필자는 그룹별로 다른 권한을 줄 것이기에, Group을 생성했다.

Group을 생성하지 않고 User에 직접 권한을 줘도 괜찮다!

 

 

📌 6. Role 부여

필자는 위에서 말했듯, 생성한 그룹에 Role을 부여했다.

  1. 생성한 [Group] 클릭 > 상단 메뉴 중 [Role mapping] 클릭 > [Assign role] 클릭
  2. [Filter]: Filter by clients
  3. 연결할 client role 선택 후 [Assign]

 

만약, User에 Role을 부여하는 경우,

  1. 생성한 [User] 클릭 > 상단 메뉴 중 [Role mapping] 클릭 > [Assign role] 클릭
  2. [Filter]: Filter by clients
  3. 연결할 client role 선택 후 [Assign]

 

📌 7. Grafana.ini 설정

https://grafana.com/docs/grafana/latest/setup-grafana/configure-security/configure-authentication/keycloak/

 

  • Grafana ConfigMap 수정
    • values.yaml 파일을 수정해서 적용할 수 있지만, 필자는 values.yaml 파일에 secret키 등을 노출시키는게 싫어서 config map을 수정했다. (helm - kube-prometheus-stack 기준)
kubectl edit cm -n prometheus kube-prometheus-stack-grafana
grafana.ini: |
	[server]
  root_url = https://<grafana-domain>
  [auth.generic_oauth]
  enabled: true
  name: Keycloak-OAuth
  allow_sign_up: true
  client_id: <client_id>
  client_secret: <client_secret>
  scopes: openid email profile offline_access roles
  auth_url: https://<keycloak-domain>/realms/<realm-name>/protocol/openid-connect/auth
  token_url: https://<keycloak-domain>/realms/<realm-name>/protocol/openid-connect/token
  api_url: https://<keycloak-domain>/realms/<realm-name>/protocol/openid-connect/userinfo
  role_attribute_path: contains(roles[*], 'admin') && 'Admin' || contains(roles[*], 'editor') && 'Editor' || 'Viewer'
  login_attribute_path: preferred_username
  name_attribute_path: name
  email_attribute_path: email
  1. client_secret
    • [Client] > 상단 탭 [Credentials] 에서 확인 가능

 

2. role_attribute_path

  • 토큰의 roles 배열로 Grafana 권한 매핑
  • ⇒ Keycloak Mapper 설정 필요!

 

📌 8. Keycloak Mapper 설정

  1. [Client] 선택 > 상단 탭 [Client scopes] 선택 > […-dedicated] 선택
  2. [Configure a new mapper] 클릭하여 새로운 Mapper 등록

  • [User Client Role] 선택
  • Client ID 선택
  • Token Claim Name: roles

 

 

짜잔 ~ 설정 끝 🎉

로그인 버튼 하단에 Sign in with Keycloak-OAuth 버튼이 생긴 것을 볼 수 있다.