1
1
package com .openshift .cloud .beans ;
2
2
3
3
import com .openshift .cloud .controllers .ConditionAwareException ;
4
+ import com .openshift .cloud .controllers .ConditionUtil ;
4
5
import com .openshift .cloud .v1alpha .models .KafkaCondition ;
5
6
import io .fabric8 .kubernetes .client .KubernetesClient ;
6
7
import io .vertx .core .json .JsonObject ;
@@ -52,9 +53,12 @@ public String getAccessToken(String accessTokenSecretName, String namespace)
52
53
var offlineToken = getOfflineTokenFromSecret (accessTokenSecretName , namespace );
53
54
var accessToken = exchangeToken (offlineToken );
54
55
return accessToken ;
56
+ } catch (ConditionAwareException ex ) {
57
+ // Log and rethrow exception
58
+ LOG .log (Level .SEVERE , ex .getMessage ());
59
+ throw ex ;
55
60
} catch (Throwable ex ) {
56
- // I do not like ^^, but it seems like one of the APIs we call throws a type "error" when it
57
- // should throw "exception"
61
+ // Unexpected exception or error (NPE, IOException, out of memory, etc)
58
62
LOG .log (Level .SEVERE , ex .getMessage ());
59
63
throw new ConditionAwareException (
60
64
ex .getMessage (),
@@ -66,24 +70,42 @@ public String getAccessToken(String accessTokenSecretName, String namespace)
66
70
}
67
71
}
68
72
69
- private String getOfflineTokenFromSecret (String secretName , String namespace ) throws Exception {
73
+ /**
74
+ * Given a secret and a namespace load the secret and decode the value with the key "value"
75
+ *
76
+ * @param secretName name of the secret
77
+ * @param namespace namespace of the secret
78
+ * @return the 64 decoded value of namespace/secretName
79
+ * @throws ConditionAwareException if the secret does not exist
80
+ * @throws IllegalArgumentException if secret value is not in valid Base64
81
+ */
82
+ private String getOfflineTokenFromSecret (String secretName , String namespace )
83
+ throws ConditionAwareException {
70
84
var token = k8sClient .secrets ().inNamespace (namespace ).withName (secretName ).get ();
71
85
if (token != null ) {
72
86
var offlineToken = token .getData ().get (ACCESS_TOKEN_SECRET_KEY );
87
+ // decode may throw IllegalArgumentException
73
88
offlineToken = new String (Base64 .getDecoder ().decode (offlineToken ));
74
-
75
89
return offlineToken ;
76
90
}
77
- throw new Exception ("Missing Offline Token Secret " + secretName );
91
+ // We expect the token to exist, and if it doesn't raise an exception.
92
+ throw new ConditionAwareException (
93
+ String .format ("Missing Offline Token Secret %s" , secretName ),
94
+ null ,
95
+ KafkaCondition .Type .AcccesTokenSecretValid ,
96
+ KafkaCondition .Status .False ,
97
+ "ConditionAwareException" ,
98
+ String .format ("Missing Offline Token Secret %s" , secretName ));
78
99
}
79
100
80
101
/**
81
102
* This method exchanges an offline token for a new refresh token
82
103
*
83
104
* @param offlineToken the token from ss.redhat.com
84
105
* @return a token to be used as a bearer token to authorize the user
106
+ * @throws ConditionAwareException
85
107
*/
86
- private String exchangeToken (String offlineToken ) {
108
+ private String exchangeToken (String offlineToken ) throws ConditionAwareException {
87
109
try {
88
110
HttpRequest request =
89
111
HttpRequest .newBuilder ()
@@ -107,10 +129,24 @@ private String exchangeToken(String offlineToken) {
107
129
var json = new JsonObject (tokens );
108
130
return json .getString ("access_token" );
109
131
} else {
110
- throw new RuntimeException (response .body ());
132
+ LOG .log (
133
+ Level .SEVERE , String .format ("Exchange token failed with error %s" , response .body ()));
134
+ throw new ConditionAwareException (
135
+ response .body (),
136
+ null ,
137
+ KafkaCondition .Type .AcccesTokenSecretValid ,
138
+ KafkaCondition .Status .False ,
139
+ String .format ("Http Error Code %d" , response .statusCode ()),
140
+ ConditionUtil .getStandarizedErrorMessage (response .statusCode ()));
111
141
}
112
142
} catch (IOException | InterruptedException e ) {
113
- throw new RuntimeException (e );
143
+ throw new ConditionAwareException (
144
+ e .getMessage (),
145
+ e ,
146
+ KafkaCondition .Type .AcccesTokenSecretValid ,
147
+ KafkaCondition .Status .False ,
148
+ e .getClass ().getName (),
149
+ e .getMessage ());
114
150
}
115
151
}
116
152
0 commit comments