Skip to content

Commit

Permalink
[HOPSWORKS-2993] Update git provider secrets throws an secret already… (
Browse files Browse the repository at this point in the history
  • Loading branch information
SirOibaf committed Feb 26, 2022
1 parent 2ccf13c commit adafb33
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 30 deletions.
31 changes: 17 additions & 14 deletions hopsworks-IT/src/test/ruby/spec/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,30 @@
expect_status_details(200)
expect(json_body[:items].count).to be == 0
end
git_providers = ['GitHub', 'GitLab', 'BitBucket']
git_providers.each do |provider_to_configure|

['GitHub', 'GitLab', 'BitBucket'].each do |provider_to_configure|
it "should indicate configured after configuration #{provider_to_configure}" do
configure_git_provider(provider_to_configure)
get_providers()
expect_status_details(200)
expect(json_body[:items].count).to be > 0
providers = json_body[:items]
is_configured = false
providers.each do |provider|
if provider[:gitProvider] == provider_to_configure
if provider[:username] != "" && provider[:token] != ""
is_configured = true
end
break
end
end
expect(is_configured).to be true
expect(json_body[:items].count).to eql 1
expect(json_body[:items][0][:gitProvider]).to eql provider_to_configure
expect(json_body[:items][0][:username]).to eql "username"
expect(json_body[:items][0][:token]).to eql "token"
delete_provider_configuration(provider_to_configure)
end
end

it "should update a provider configuration" do
configure_git_provider("GitHub", token="new token")
get_providers()
expect_status_details(200)
expect(json_body[:items].count).to eql 1
expect(json_body[:items][0][:gitProvider]).to eql "GitHub"
expect(json_body[:items][0][:username]).to eql "username"
expect(json_body[:items][0][:token]).to eql "new token"
delete_provider_configuration("GitHub")
end
end
describe "Cloning repositories" do
git_providers = ['GitHub', 'GitLab']
Expand Down
17 changes: 6 additions & 11 deletions hopsworks-IT/src/test/ruby/spec/helpers/git_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@
=end

module GitHelper
def configure_git_provider(git_provider)
if git_provider == "GitLab"
add_private_secret("gitlab_token", "token")
add_private_secret("gitlab_username", "gitlab_username")
elsif git_provider == "GitHub"
add_private_secret("github_token", "token")
add_private_secret("github_username", "github_username")
elsif git_provider == "BitBucket"
add_private_secret("bitbucket_token", "token")
add_private_secret("bitbucket_username", "bitbucket_username")
end
def configure_git_provider(git_provider, token="token")
post "#{ENV['HOPSWORKS_API']}/users/git/provider", {
gitProvider: git_provider,
username: 'username',
token: token,
}
end

def delete_provider_configuration(git_provider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public Response gitProviders(@Context SecurityContext sc, @Context UriInfo uriIn
return Response.ok().entity(dto).build();
}

@ApiOperation(value = "Get git providers with the configured secrets", response= GitProviderSecretsDTO.class)
@ApiOperation(value = "Configure a Git Provider secrets", response= GitProviderSecretsDTO.class)
@POST
@Path("/git/provider")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ public void createAuthenticationSecret(Users user, String username, String token
throw new IllegalArgumentException("Unsupported git provider");
}

secretsController.add(user, usernameSecretName, username, VisibilityType.PRIVATE, null);
secretsController.add(user, tokenSecretName, token, VisibilityType.PRIVATE, null);
secretsController.addOrUpdate(user, usernameSecretName, username, VisibilityType.PRIVATE, null);
secretsController.addOrUpdate(user, tokenSecretName, token, VisibilityType.PRIVATE, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class SecretsController {
* @throws UserException
*/
public Secret add(Users user, String secretName, String secret, VisibilityType visibilityType, Integer projectIdScope)
throws UserException {
throws UserException {
SecretId secretId = new SecretId(user.getUid(), secretName);
if(secretsFacade.findById(secretId) != null) {
throw new UserException(RESTCodes.UserErrorCode.SECRET_EXISTS, Level.FINE,
Expand All @@ -96,7 +96,37 @@ public Secret add(Users user, String secretName, String secret, VisibilityType v
secretsFacade.persist(storedSecret);
return storedSecret;
}


/**
* Adds a new Secret. The secret is encrypted before persisted in the database.
* If a secret with the same name already exists for the user, it updates it.
*
* @param user
* @param secretName
* @param secretStr
* @param visibilityType
* @param projectIdScope
* @return
*/
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public Secret addOrUpdate(Users user, String secretName, String secretStr,
VisibilityType visibilityType, Integer projectIdScope) throws UserException {
SecretId secretId = new SecretId(user.getUid(), secretName);
Secret secret = secretsFacade.findById(secretId);
if (secret != null) {
Secret generatedSecret = validateAndCreateSecret(secretId, user, secretStr, visibilityType, projectIdScope);
secret.setSecret(generatedSecret.getSecret());
secret.setAddedOn(generatedSecret.getAddedOn());
secret.setVisibilityType(generatedSecret.getVisibilityType());
secret.setProjectIdScope(generatedSecret.getProjectIdScope());
} else {
secret = validateAndCreateSecret(secretId, user, secretStr, visibilityType, projectIdScope);
}

secretsFacade.persist(secret);
return secret;
}

/**
*
* @param user
Expand Down

0 comments on commit adafb33

Please sign in to comment.