Skip to content

Commit e6bc656

Browse files
committed
Added environment auth provider with runtime class loading
1 parent ebe9f19 commit e6bc656

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tech.ydb.auth;
2+
3+
import java.nio.file.Path;
4+
5+
import tech.ydb.core.impl.auth.GrpcAuthRpc;
6+
7+
/**
8+
*
9+
* @author Aleksandr Gorshenin
10+
*/
11+
public class OAuth2AuthHelper {
12+
private OAuth2AuthHelper() { }
13+
14+
public static AuthIdentity configFileIdentity(Path file, GrpcAuthRpc rpc) {
15+
return OAuth2TokenExchangeProvider
16+
.fromFile(file.toFile())
17+
.build()
18+
.createAuthIdentity(rpc);
19+
}
20+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package tech.ydb.core.auth;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
import tech.ydb.auth.AuthIdentity;
9+
import tech.ydb.auth.AuthRpcProvider;
10+
import tech.ydb.core.impl.auth.GrpcAuthRpc;
11+
12+
/**
13+
*
14+
* @author Aleksandr Gorshenin
15+
*/
16+
public class EnvironAuthProvider implements AuthRpcProvider<GrpcAuthRpc> {
17+
private static final String IAM_CLASS_NAME = "tech.ydb.auth.iam.CloudAuthIdentity";
18+
private static final String IAM_CLASS_ERROR = "Cannot find CloudAuthIdentity class, "
19+
+ "you have to add tech.ydb.auth:yc-auth-provider artifact to classpath";
20+
21+
private static final String OAUTH2_CLASS_NAME = "tech.ydb.auth.OAuth2AuthHelper";
22+
private static final String OAUTH2_CLASS_ERROR = "Cannot find OAuth2AuthHelper class, "
23+
+ "you have to add tech.ydb.auth:ydb-oauth2-provider artifact to classpath";
24+
25+
@Override
26+
public AuthIdentity createAuthIdentity(GrpcAuthRpc rpc) {
27+
String anonCredentials = System.getenv("YDB_ANONYMOUS_CREDENTIALS");
28+
if (anonCredentials != null && anonCredentials.equals("1")) {
29+
return null;
30+
}
31+
32+
String saKeyFile = System.getenv("YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS");
33+
if (saKeyFile != null) {
34+
return loadServiceAccountIdentity(Paths.get(saKeyFile));
35+
}
36+
37+
String metadataCredentials = System.getenv("YDB_METADATA_CREDENTIALS");
38+
if (metadataCredentials != null && metadataCredentials.equals("1")) {
39+
return loadMetadataIdentity();
40+
}
41+
42+
String accessToken = System.getenv("YDB_ACCESS_TOKEN_CREDENTIALS");
43+
if (accessToken != null) {
44+
return () -> accessToken;
45+
}
46+
47+
String oauth2KeyFile = System.getenv("YDB_OAUTH2_KEY_FILE");
48+
if (oauth2KeyFile != null) {
49+
return loadOAuth2KeyProvider(rpc, Paths.get(oauth2KeyFile));
50+
}
51+
52+
return loadMetadataIdentity();
53+
}
54+
55+
private static AuthIdentity loadServiceAccountIdentity(Path saKeyFile) {
56+
try {
57+
Class<?> clazz = Class.forName(IAM_CLASS_NAME);
58+
Method method = clazz.getMethod("serviceAccountIdentity", Path.class, String.class);
59+
return (AuthIdentity) method.invoke(null, saKeyFile, null);
60+
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException |
61+
IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
62+
throw new RuntimeException(IAM_CLASS_ERROR, ex);
63+
}
64+
}
65+
66+
private static AuthIdentity loadMetadataIdentity() {
67+
try {
68+
Class<?> clazz = Class.forName(IAM_CLASS_NAME);
69+
Method method = clazz.getMethod("metadataIdentity", String.class);
70+
return (AuthIdentity) method.invoke(null, (Object) null);
71+
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException |
72+
IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
73+
throw new RuntimeException(IAM_CLASS_ERROR, ex);
74+
}
75+
}
76+
77+
private static AuthIdentity loadOAuth2KeyProvider(GrpcAuthRpc rpc, Path configFile) {
78+
try {
79+
Class<?> clazz = Class.forName(OAUTH2_CLASS_NAME);
80+
Method method = clazz.getMethod("configFileIdentity", Path.class, GrpcAuthRpc.class);
81+
return (AuthIdentity) method.invoke(null, configFile, rpc);
82+
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException |
83+
IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
84+
throw new RuntimeException(OAUTH2_CLASS_ERROR, ex);
85+
}
86+
}
87+
}

core/src/main/java/tech/ydb/core/auth/JwtUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @author Aleksandr Gorshenin
1616
*/
17-
public class JwtUtils {
17+
class JwtUtils {
1818
private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class);
1919

2020
private static final char SEPARATOR_CHAR = '.';

0 commit comments

Comments
 (0)