|
| 1 | +package com.odysseusinc.arachne.executionengine.auth; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import com.odysseusinc.arachne.commons.types.DBMSType; |
| 5 | +import com.odysseusinc.arachne.execution_engine_common.api.v1.dto.DataSourceUnsecuredDTO; |
| 6 | +import lombok.AllArgsConstructor; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.apache.commons.io.FileUtils; |
| 9 | +import org.apache.commons.io.IOUtils; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.OutputStream; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.nio.file.Paths; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +@Service |
| 20 | +@Slf4j |
| 21 | +public class BigQueryCredentialsProvider implements CredentialsProvider { |
| 22 | + private static final String RUNTIME_BQ_KEYFILE = "BQ_KEYFILE"; |
| 23 | + |
| 24 | + @Override |
| 25 | + public AuthEffects apply(DataSourceUnsecuredDTO dataSource, Path analysisDir, String analysisDirInContainer) { |
| 26 | + if (dataSource.getType() == DBMSType.BIGQUERY) { |
| 27 | + try { |
| 28 | + Path keyFile = Files.createTempFile(analysisDir, "", ".json").toAbsolutePath(); |
| 29 | + try (OutputStream out = Files.newOutputStream(keyFile)) { |
| 30 | + IOUtils.write(dataSource.getKeyfile(), out); |
| 31 | + } |
| 32 | + Path innerPath = Paths.get(analysisDirInContainer).resolve(keyFile.getFileName()); |
| 33 | + return new Effects(dataSource.getConnectionString(), keyFile, innerPath.toString()); |
| 34 | + } catch (IOException e) { |
| 35 | + log.error("Failed to resolve BigQuery authentication for Source: [{}]", dataSource.getName(), e); |
| 36 | + throw new RuntimeException(e); |
| 37 | + } |
| 38 | + } else { |
| 39 | + return null; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @AllArgsConstructor |
| 44 | + private static class Effects implements AuthEffects, AuthEffects.AddEnvironmentVariables, AuthEffects.ModifyUrl, AuthEffects.Cleanup { |
| 45 | + private final String newUrl; |
| 46 | + private final Path keyFile; |
| 47 | + private final String keyFileInContainer; |
| 48 | + |
| 49 | + @Override |
| 50 | + public Map<String, String> getEnvVars() { |
| 51 | + return ImmutableMap.of(RUNTIME_BQ_KEYFILE, keyFileInContainer); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getNewUrl() { |
| 56 | + return newUrl + ";OAuthPvtKeyPath=" + keyFileInContainer + ";"; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void cleanup() { |
| 61 | + FileUtils.deleteQuietly(keyFile.toFile()); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments