Invalid redirect uri in keycloak after after clicking on sign in with keycloak on grafana

  • What Grafana version and what operating system are you using?
    I am using Linux VM on azure and grafana v9.5.5 on docker

  • What are you trying to achieve?
    I am trying to authenticate Grafana with keycloak AD.

  • How are you trying to achieve it?
    I am integrating (configuring) keycloak with grafana I have done the configuration like you can see these configuration

#################################### Generic OAuth ##########################                                                                                                                                                      
[auth.generic_oauth]                                                                                                                                                                                                               
enabled = true                                                                                                                                                                                                                     
name = Keycloak-OAuth                                                                                                                                                                                                              
allow_sign_up = true                                                                                                                                                                                                               
;auto_login = false                                                                                                                                                                                                                
client_id = grafana-oauth                                                                                                                                                                                                          
client_secret = edY0A21uYADOlm9Tm6w8ThsWwZMSIOHR                                                                                                                                                                                   
scopes = openid email profile offline_access roles                                                                                                                                                                                 
;empty_scopes = false                                                                                                                                                                                                              
;email_attribute_name = email:primary                                                                                                                                                                                              
email_attribute_path = email                                                                                                                                                                                                       
login_attribute_path = username                                                                                                                                                                                                    
name_attribute_path = full_name                                                                                                                                                                                                    
;id_token_attribute_name =                                                                                                                                                                                                         
auth_url = https://keycloak.mynew.com:8443/realms/AD/protocol/openid-connect/auth                                                                                                                                                  
token_url = https://keycloak.mynew.com:8443/realms/AD/protocol/openid-connect/token                                                                                                                                                
api_url = https://keycloak.mynew.com:8443/realms/AD/protocol/openid-connect/userinfo                                                                                                                                               
;teams_url =                                                                                                                                                                                                                       
;allowed_domains =                                                                                                                                                                                                                 
;team_ids =                                                                                                                                                                                                                        
;allowed_organizations =                                                                                                                                                                                                           
role_attribute_path = contains(roles[*], 'admin') && 'Admin' || contains(roles[*], 'editor') && 'Editor' || 'Viewer'                                                                                                               
;role_attribute_strict = false                                                                                                                                                                                                     
groups_attribute_path = groups                                                                                                                                                                                                     
;team_ids_attribute_path =                                                                                                                                                                                                         
;tls_skip_verify_insecure = false                                                                                                                                                                                                  
;tls_client_cert =                                                                                                                                                                                                                 
;tls_client_key =                                                                                                                                                                                                                  
;tls_client_ca =                                                                                                                                                                                                                   
;use_pkce = false                                                                                                                                                                                                                  
;auth_style =                                                                                                                                                                                                                      
allow_assign_grafana_admin = true

getting this error in keycloak container logs
2023-08-06 07:48:04,029 WARN [org.keycloak.events] (executor-thread-242) type=LOGIN_ERROR, realmId=323fec05-a250-4497-84f7-95fa2654b97e, clientId=grafana-oauth, userId=null, ipAddress=172.190.104.173, error=invalid_redirect_uri, redirect_uri=grafana.mynew.com:7443/login/generic_oauth

  • What did you expect to happen?
    It should open the login page on keycloak.

  • Can you copy/paste the configuration(s) that you are having problems with?
    Yes, I have pasted the grafana cong above

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    Here are the grafana logs
    logger=context userId=0 orgId=0 uname= t=2023-08-06T07:57:24.171323458Z level=info msg="Request Completed" method=GET path=/login/generic_oauth status=302 remote_addr=172.190.104.173 time_ms=0 duration=643.113µs size=315 referer=https://grafana.mynew.com:7443/login handler=/login/:name

  • Did you follow any online instructions? If so, what is the URL?
    Configure Keycloak OAuth2 authentication | Grafana documentation

You didn’t follow doc properly:

1.) Create a client in Keycloak with the following settings:

Valid Redirect URIs: <grafana_root_url>/*

As an example, <grafana_root_url> can be https://play.grafana.org.

I have done a similar @jangaraj I have attached the image, Please have a look your suggestions will be appreciated Thank you in advance!

For the callback URL to be correct, it might be necessary to set the root_url option in the [server]section of the Grafana configuration file.

Please have a look at this video

Hi @jangaraj , Yes I have tried to do change according to you I have added configuration in [ server ] option on grafana.ini file but after making change when I restart the container It gets terminated automatically.

Let’s make it visual. Which redirect URI was generarted by Grafana? grafana.mynew.com:7443/login/generic_oauth

Which redirect URI is allowed in Keycloak client config? https://grafana.mynew.com:7443/*

Does grafana.mynew.com:7443/login/generic_oauth match allowed value https://grafana.mynew.com:7443/* → No, so Keycloak will reject it.

What control which redirect uri is generated in Grafana?

For the callback URL to be correct, it might be necessary to set the root_url option in the [server]section of the Grafana configuration file.

What did you configure there? You didn’t show.
Why your Grafana is not able start with new unknown config? You didn’t check Grafana logs, there can be million things wrong. So check the logs first. I bet you have some typo, invalid syntax, …

Hi @jangaraj, Hope you are doing well! I have set the root_url in [ server ] in grafana file please have a look at the attached video. Your suggestions will be appreciated, Thanks in advance :slight_smile:

Now when I give this URL in valid redirect uri as show in picture below than it works but authentication fails I have attached the video. Please have a look.