Search in sources :

Example 1 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class EncryptAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkIsSystemAdministrator();
    String value = request.mandatoryParam(PARAM_VALUE);
    Encryption encryption = settings.getEncryption();
    checkRequest(encryption.hasSecretKey(), "No secret key available");
    String encryptedValue = encryption.encrypt(value);
    writeProtobuf(toEncryptWsResponse(encryptedValue), request, response);
}
Also used : Encryption(org.sonar.api.config.internal.Encryption)

Example 2 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class CreateBitbucketActionTest method create.

@Test
public void create() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user).setSystemAdministrator();
    ws.newRequest().setParam("key", "Bitbucket Server - Dev Team").setParam("url", "https://bitbucket.enterprise.com").setParam("personalAccessToken", "98765432100").execute();
    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())).extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption)).containsOnly(tuple("Bitbucket Server - Dev Team", "https://bitbucket.enterprise.com", "98765432100"));
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) ComponentFinder(org.sonar.server.component.ComponentFinder) UserDto(org.sonar.db.user.UserDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultipleAlmFeatureProvider(org.sonar.server.almsettings.MultipleAlmFeatureProvider) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) BadRequestException(org.sonar.server.exceptions.BadRequestException) Rule(org.junit.Rule) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Encryption(org.sonar.api.config.internal.Encryption) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) UserDto(org.sonar.db.user.UserDto) Test(org.junit.Test)

Example 3 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class CreateGithubActionTest method create.

@Test
public void create() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user).setSystemAdministrator();
    ws.newRequest().setParam("key", "GitHub Server - Dev Team").setParam("url", "https://github.enterprise.com").setParam("appId", "12345").setParam("privateKey", "678910").setParam("clientId", "client_1234").setParam("clientSecret", "client_so_secret").execute();
    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())).extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getAppId, s -> s.getDecryptedPrivateKey(encryption), AlmSettingDto::getClientId, s -> s.getDecryptedClientSecret(encryption)).containsOnly(tuple("GitHub Server - Dev Team", "https://github.enterprise.com", "12345", "678910", "client_1234", "client_so_secret"));
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) ComponentFinder(org.sonar.server.component.ComponentFinder) UserDto(org.sonar.db.user.UserDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultipleAlmFeatureProvider(org.sonar.server.almsettings.MultipleAlmFeatureProvider) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) BadRequestException(org.sonar.server.exceptions.BadRequestException) Rule(org.junit.Rule) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Encryption(org.sonar.api.config.internal.Encryption) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) UserDto(org.sonar.db.user.UserDto) Test(org.junit.Test)

Example 4 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class ListGithubOrganizationsActionTest method return_organizations_overriding_existing_personal_access_token.

@Test
public void return_organizations_overriding_existing_personal_access_token() {
    AlmSettingDto githubAlmSettings = setupAlm();
    // old pat
    AlmPatDto pat = db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSettings.getUuid()).setUserUuid(userSession.getUuid()));
    // new pat
    UserAccessToken accessToken = new UserAccessToken("token_for_abc");
    when(appClient.createUserAccessToken(githubAlmSettings.getUrl(), githubAlmSettings.getClientId(), githubAlmSettings.getDecryptedClientSecret(encryption), "abc")).thenReturn(accessToken);
    setupGhOrganizations(githubAlmSettings, accessToken.getValue());
    ListGithubOrganizationsWsResponse response = ws.newRequest().setParam(PARAM_ALM_SETTING, githubAlmSettings.getKey()).setParam(PARAM_TOKEN, "abc").executeProtobuf(ListGithubOrganizationsWsResponse.class);
    assertThat(response.getPaging()).extracting(Common.Paging::getPageIndex, Common.Paging::getPageSize, Common.Paging::getTotal).containsOnly(1, 100, 2);
    assertThat(response.getOrganizationsList()).extracting(GithubOrganization::getKey, GithubOrganization::getName).containsOnly(tuple("github", "github"), tuple("octacat", "octacat"));
    verify(appClient).createUserAccessToken(githubAlmSettings.getUrl(), githubAlmSettings.getClientId(), githubAlmSettings.getDecryptedClientSecret(encryption), "abc");
    verify(appClient).listOrganizations(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(accessToken.getValue())), eq(1), eq(100));
    Mockito.verifyNoMoreInteractions(appClient);
    assertThat(db.getDbClient().almPatDao().selectByUserAndAlmSetting(db.getSession(), userSession.getUuid(), githubAlmSettings).get().getPersonalAccessToken()).isEqualTo(accessToken.getValue());
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GithubOrganization(org.sonarqube.ws.AlmIntegrations.GithubOrganization) Settings(org.sonar.api.config.internal.Settings) BeforeClass(org.junit.BeforeClass) UserDto(org.sonar.db.user.UserDto) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) GlobalPermission(org.sonar.db.permission.GlobalPermission) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GithubApplicationClient(org.sonar.alm.client.github.GithubApplicationClient) GithubApplicationClientImpl(org.sonar.alm.client.github.GithubApplicationClientImpl) PARAM_TOKEN(org.sonar.server.almintegration.ws.github.ListGithubOrganizationsAction.PARAM_TOKEN) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) BadRequestException(org.sonar.server.exceptions.BadRequestException) PARAM_ALM_SETTING(org.sonar.server.almintegration.ws.github.ListGithubOrganizationsAction.PARAM_ALM_SETTING) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ListGithubOrganizationsWsResponse(org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse) UserSessionRule.standalone(org.sonar.server.tester.UserSessionRule.standalone) Encryption(org.sonar.api.config.internal.Encryption) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) Collectors(java.util.stream.Collectors) NotFoundException(org.sonar.server.exceptions.NotFoundException) Common(org.sonarqube.ws.Common) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Stream(java.util.stream.Stream) Rule(org.junit.Rule) UnauthorizedException(org.sonar.server.exceptions.UnauthorizedException) Mockito.mock(org.mockito.Mockito.mock) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ListGithubOrganizationsWsResponse(org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse) Common(org.sonarqube.ws.Common) Test(org.junit.Test)

Example 5 with Encryption

use of org.sonar.api.config.internal.Encryption in project sonarqube by SonarSource.

the class CreateAzureActionTest method create.

@Test
public void create() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user).setSystemAdministrator();
    ws.newRequest().setParam("key", "Azure Server - Dev Team").setParam("personalAccessToken", "98765432100").setParam("url", "https://ado.sonarqube.com/").execute();
    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())).extracting(AlmSettingDto::getKey, s -> s.getDecryptedPersonalAccessToken(encryption), AlmSettingDto::getUrl).containsOnly(tuple("Azure Server - Dev Team", "98765432100", "https://ado.sonarqube.com/"));
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) ComponentFinder(org.sonar.server.component.ComponentFinder) UserDto(org.sonar.db.user.UserDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultipleAlmFeatureProvider(org.sonar.server.almsettings.MultipleAlmFeatureProvider) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) BadRequestException(org.sonar.server.exceptions.BadRequestException) Rule(org.junit.Rule) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Encryption(org.sonar.api.config.internal.Encryption) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) UserDto(org.sonar.db.user.UserDto) Test(org.junit.Test)

Aggregations

Encryption (org.sonar.api.config.internal.Encryption)36 Test (org.junit.Test)35 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)30 Rule (org.junit.Rule)30 Mockito.mock (org.mockito.Mockito.mock)30 DbTester (org.sonar.db.DbTester)30 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)26 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)26 UserDto (org.sonar.db.user.UserDto)26 UserSessionRule (org.sonar.server.tester.UserSessionRule)26 WsActionTester (org.sonar.server.ws.WsActionTester)26 Tuple.tuple (org.assertj.core.groups.Tuple.tuple)25 WebService (org.sonar.api.server.ws.WebService)25 MultipleAlmFeatureProvider (org.sonar.server.almsettings.MultipleAlmFeatureProvider)25 ComponentFinder (org.sonar.server.component.ComponentFinder)25 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)25 Mockito.when (org.mockito.Mockito.when)20 NotFoundException (org.sonar.server.exceptions.NotFoundException)20 String.format (java.lang.String.format)17 TestRequest (org.sonar.server.ws.TestRequest)17