1
- package org .apache .atlas .keycloak .client ;
1
+ package org .apache .atlas .auth .client . auth ;
2
2
3
+ import com .fasterxml .jackson .databind .DeserializationFeature ;
3
4
import com .fasterxml .jackson .databind .ObjectMapper ;
4
5
import io .micrometer .core .instrument .Timer ;
6
+ import org .apache .atlas .auth .client .config .AuthConfig ;
7
+ import org .apache .atlas .auth .client .heracles .RetrofitHeraclesClient ;
8
+ import org .apache .atlas .auth .client .keycloak .RetrofitKeycloakClient ;
5
9
import okhttp3 .*;
6
10
import okhttp3 .logging .HttpLoggingInterceptor ;
7
11
import org .apache .atlas .AtlasErrorCode ;
8
12
import org .apache .atlas .exception .AtlasBaseException ;
9
- import org .apache .atlas .keycloak .client .config .KeycloakConfig ;
10
- import org .apache .atlas .keycloak .client .service .AtlasKeycloakAuthService ;
11
13
import org .apache .atlas .service .metrics .MetricUtils ;
12
14
import org .slf4j .Logger ;
13
15
import org .slf4j .LoggerFactory ;
26
28
import static org .apache .atlas .AtlasErrorCode .BAD_REQUEST ;
27
29
import static org .apache .atlas .AtlasErrorCode .RESOURCE_NOT_FOUND ;
28
30
29
- abstract class AbstractKeycloakClient {
31
+ public class AbstractAuthClient {
30
32
31
- private final static Logger LOG = LoggerFactory .getLogger (AbstractKeycloakClient .class );
33
+ private final static Logger LOG = LoggerFactory .getLogger (AbstractAuthClient .class );
32
34
private static final Map <Integer , AtlasErrorCode > ERROR_CODE_MAP = new HashMap <>();
33
35
34
- private static final int DEFAULT_KEYCLOAK_RETRY = 3 ;
36
+ private static final int DEFAULT_RETRY = 3 ;
35
37
private static final String AUTHORIZATION = "Authorization" ;
36
38
private static final String BEARER = "Bearer " ;
37
39
private static final int TIMEOUT_IN_SEC = 60 ;
38
40
private static final String INTEGRATION = "integration" ;
39
41
private static final String KEYCLOAK = "keycloak" ;
40
42
41
- protected final KeycloakConfig keycloakConfig ;
42
- protected final RetrofitKeycloakClient retrofit ;
43
+ protected final AuthConfig authConfig ;
44
+ protected final RetrofitKeycloakClient retrofitKeycloakClient ;
45
+ protected final RetrofitHeraclesClient retrofitHeraclesClient ;
43
46
44
- private final AtlasKeycloakAuthService authService ;
47
+ private final KeycloakAuthenticationService authService ;
45
48
private MetricUtils metricUtils = null ;
46
49
47
50
static {
48
51
ERROR_CODE_MAP .put (HTTP_NOT_FOUND , RESOURCE_NOT_FOUND );
49
52
ERROR_CODE_MAP .put (HTTP_BAD_REQUEST , BAD_REQUEST );
50
53
}
51
54
52
- public AbstractKeycloakClient ( KeycloakConfig keycloakConfig ) {
53
- this .keycloakConfig = keycloakConfig ;
55
+ public AbstractAuthClient ( AuthConfig authConfig ) {
56
+ this .authConfig = authConfig ;
54
57
this .metricUtils = new MetricUtils ();
55
58
HttpLoggingInterceptor httpInterceptor = new HttpLoggingInterceptor ();
56
59
httpInterceptor .setLevel (HttpLoggingInterceptor .Level .BODY );
@@ -64,11 +67,15 @@ public AbstractKeycloakClient(KeycloakConfig keycloakConfig) {
64
67
.writeTimeout (TIMEOUT_IN_SEC , TimeUnit .SECONDS )
65
68
.readTimeout (TIMEOUT_IN_SEC , TimeUnit .SECONDS )
66
69
.build ();
67
- this .retrofit = new Retrofit .Builder ().client (okHttpClient )
68
- .baseUrl (this .keycloakConfig .getAuthServerUrl ())
70
+ this .retrofitKeycloakClient = new Retrofit .Builder ().client (okHttpClient )
71
+ .baseUrl (this .authConfig .getAuthServerUrl ())
69
72
.addConverterFactory (JacksonConverterFactory .create (new ObjectMapper ())).build ()
70
73
.create (RetrofitKeycloakClient .class );
71
- authService = new AtlasKeycloakAuthService (keycloakConfig );
74
+ this .retrofitHeraclesClient = new Retrofit .Builder ().client (okHttpClient )
75
+ .baseUrl (this .authConfig .getHeraclesApiServerUrl ())
76
+ .addConverterFactory (JacksonConverterFactory .create (new ObjectMapper ().disable (DeserializationFeature .FAIL_ON_IGNORED_PROPERTIES ))).build ()
77
+ .create (RetrofitHeraclesClient .class );
78
+ authService = new KeycloakAuthenticationService (authConfig );
72
79
}
73
80
74
81
/**
@@ -97,21 +104,20 @@ public Response intercept(@NonNull Chain chain) throws IOException {
97
104
return chain .proceed (request );
98
105
}
99
106
};
100
-
101
107
/**
102
108
* Called only during auth failures.
103
109
*/
104
110
Authenticator authInterceptor = new Authenticator () {
105
111
@ Override
106
112
public Request authenticate (Route route , @ NonNull Response response ) {
107
- if (responseCount (response ) > DEFAULT_KEYCLOAK_RETRY ) {
108
- LOG .warn ("Keycloak : Falling back, retried {} times" , DEFAULT_KEYCLOAK_RETRY );
113
+ if (responseCount (response ) > DEFAULT_RETRY ) {
114
+ LOG .warn ("Auth Client : Falling back, retried {} times" , DEFAULT_RETRY );
109
115
return null ;
110
116
}
111
- LOG .info ("Keycloak : Current keycloak token status, Expired: {}" , authService .isTokenExpired ());
117
+ LOG .info ("Auth Client : Current keycloak token status, Expired: {}" , authService .isTokenExpired ());
112
118
return response .request ().newBuilder ()
113
- .addHeader (AUTHORIZATION , BEARER + authService .getAuthToken ())
114
- .build ();
119
+ .addHeader (AUTHORIZATION , BEARER + authService .getAuthToken ())
120
+ .build ();
115
121
}
116
122
117
123
private int responseCount (Response response ) {
@@ -134,13 +140,14 @@ protected <T> retrofit2.Response<T> processResponse(retrofit2.Call<T> req) throw
134
140
return response ;
135
141
}
136
142
String errMsg = response .errorBody ().string ();
137
- LOG .error ("Keycloak : Client request processing failed code {} message:{}, request: {} {}" ,
143
+ LOG .error ("Auth Client : Client request processing failed code {} message:{}, request: {} {}" ,
138
144
response .code (), errMsg , req .request ().method (), req .request ().url ());
139
145
throw new AtlasBaseException (ERROR_CODE_MAP .getOrDefault (response .code (), BAD_REQUEST ), errMsg );
140
146
} catch (Exception e ) {
141
- LOG .error ("Keycloak : request failed, request: {} {}, Exception: {}" , req .request ().method (), req .request ().url (), e );
142
- throw new AtlasBaseException (BAD_REQUEST , "Keycloak request failed" );
147
+ LOG .error ("Auth Client : request failed, request: {} {}, Exception: {}" , req .request ().method (), req .request ().url (), e );
148
+ throw new AtlasBaseException (BAD_REQUEST , "Auth request failed" );
143
149
}
144
150
}
145
151
152
+
146
153
}
0 commit comments