Skip to content

Commit

Permalink
✨ : manage oauth2 access to clone git projects
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Oct 16, 2019
1 parent 9dddbcf commit 9d8a98c
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 63 deletions.
25 changes: 22 additions & 3 deletions src/main/java/io/codeka/gaia/runner/StackCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.mustachejava.Mustache;
import io.codeka.gaia.modules.bo.TerraformModule;
import io.codeka.gaia.registries.RegistryOAuth2Provider;
import io.codeka.gaia.settings.bo.Settings;
import io.codeka.gaia.stacks.bo.Stack;
import io.codeka.gaia.stacks.bo.mustache.TerraformScript;
Expand All @@ -11,6 +12,7 @@

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.function.BiFunction;

/**
Expand All @@ -20,21 +22,38 @@
public class StackCommandBuilder {

private Settings settings;

private Mustache terraformMustache;
private List<RegistryOAuth2Provider> registryOAuth2Providers;

@Autowired
StackCommandBuilder(Settings settings, Mustache terraformMustache) {
StackCommandBuilder(Settings settings, Mustache terraformMustache, List<RegistryOAuth2Provider> registryOAuth2Providers) {
this.settings = settings;
this.terraformMustache = terraformMustache;
this.registryOAuth2Providers = registryOAuth2Providers;
}

/**
* Returns the url of the git repository filled with OAuth2 token if available
*/
private String evalGitRepositoryUrl(TerraformModule module) {
var url = module.getGitRepositoryUrl();
var data = module.getCreatedBy().getOAuth2User();
if (data == null) {
return url;
}
return registryOAuth2Providers.stream()
.filter(p -> p.isAssignableFor(data.getProvider()))
.map(p -> p.getOAuth2Url(url, data.getToken()))
.findFirst()
.orElse(url);
}

private String buildScript(Stack stack, TerraformModule module,
BiFunction<Stack, TerraformModule, String> command) {
var script = new TerraformScript()
.setExternalUrl(settings.getExternalUrl())
.setStackId(stack.getId())
.setGitRepositoryUrl(module.getGitRepositoryUrl());
.setGitRepositoryUrl(evalGitRepositoryUrl(module));

if (StringUtils.isNotBlank(module.getDirectory())) {
script.setGitDirectory(module.getDirectory());
Expand Down
Loading

0 comments on commit 9d8a98c

Please sign in to comment.