|
| 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 | +} |
0 commit comments