import org.sonar.api.PropertyType;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.PropertyDefinition;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
import org.sonar.server.property.InternalProperties;
import static java.lang.String.format;
private final Configuration configuration;
private final InternalProperties internalProperties;
+ private final DbClient dbClient;
- public GitHubSettings(Configuration configuration, InternalProperties internalProperties) {
+ public GitHubSettings(Configuration configuration, InternalProperties internalProperties, DbClient dbClient) {
this.configuration = configuration;
this.internalProperties = internalProperties;
+ this.dbClient = dbClient;
}
String clientId() {
public void setProvisioning(boolean enableProvisioning) {
if (enableProvisioning) {
checkGithubConfigIsCompleteForProvisioning();
+ } else {
+ removeExternalGroupsForGithub();
}
internalProperties.write(PROVISIONING, String.valueOf(enableProvisioning));
}
+ private void removeExternalGroupsForGithub() {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ dbClient.externalGroupDao().deleteByExternalIdentityProvider(dbSession, GitHubIdentityProvider.KEY);
+ dbSession.commit();
+ }
+ }
+
private void checkGithubConfigIsCompleteForProvisioning() {
checkState(isEnabled(), getErrorMessage("GitHub authentication must be enabled"));
checkState(isNotBlank(appId()), getErrorMessage("Application ID must be provided"));
import org.junit.Test;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.server.authentication.OAuth2IdentityProvider;
+import org.sonar.db.DbClient;
import org.sonar.server.property.InternalProperties;
import static org.assertj.core.api.Assertions.assertThat;
private MapSettings settings = new MapSettings();
private InternalProperties internalProperties = mock(InternalProperties.class);
- private GitHubSettings gitHubSettings = new GitHubSettings(settings.asConfig(), internalProperties);
+ private GitHubSettings gitHubSettings = new GitHubSettings(settings.asConfig(), internalProperties, mock(DbClient.class));
private UserIdentityFactoryImpl userIdentityFactory = mock(UserIdentityFactoryImpl.class);
private ScribeGitHubApi scribeApi = new ScribeGitHubApi(gitHubSettings);
private GitHubRestClient gitHubRestClient = new GitHubRestClient(gitHubSettings);
package org.sonar.auth.github;
import java.util.Optional;
+import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.user.GroupDto;
import org.sonar.server.property.InternalProperties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
public class GitHubSettingsTest {
+ @Rule
+ public DbTester db = DbTester.create(System2.INSTANCE);
private MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, GitHubSettings.definitions()));
private InternalProperties internalProperties = mock(InternalProperties.class);
- private GitHubSettings underTest = new GitHubSettings(settings.asConfig(), internalProperties);
+ private GitHubSettings underTest = new GitHubSettings(settings.asConfig(), internalProperties, db.getDbClient());
@Test
public void is_enabled() {
}
@Test
- public void setProvisioning_whenPassedFalse_delegatesToInternalPropertiesWrite() {
+ public void setProvisioning_whenPassedFalse_delegatesToInternalPropertiesWriteAndCleansUpExternalGroups() {
+ GroupDto groupDto = createGithubManagedGroup();
+
underTest.setProvisioning(false);
+
verify(internalProperties).write(GitHubSettings.PROVISIONING, Boolean.FALSE.toString());
+ assertThat(db.getDbClient().externalGroupDao().selectByGroupUuid(db.getSession(), groupDto.getUuid())).isEmpty();
}
+ private GroupDto createGithubManagedGroup() {
+ GroupDto groupDto = db.users().insertGroup();
+ db.users().markGroupAsGithubManaged(groupDto.getUuid());
+ return groupDto;
+ }
+
+
@Test
public void return_client_id() {
settings.setProperty("sonar.auth.github.clientId.secured", "id");
// load settings with default values
private MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, GitHubSettings.definitions()));
private InternalProperties internalProperties = new InternalPropertiesImpl(db.getDbClient());
- private GitHubSettings gitHubSettings = new GitHubSettings(settings.asConfig(), internalProperties);
+ private GitHubSettings gitHubSettings = new GitHubSettings(settings.asConfig(), internalProperties, db.getDbClient());
private UserIdentityFactoryImpl userIdentityFactory = new UserIdentityFactoryImpl();
private ScribeGitHubApi scribeApi = new ScribeGitHubApi(gitHubSettings);
private GitHubRestClient gitHubRestClient = new GitHubRestClient(gitHubSettings);
private static final String GROUP_UUID = "uuid";
private static final String EXTERNAL_ID = "external_id";
private static final String EXTERNAL_IDENTITY_PROVIDER = "external_identity_provider";
+ private static final String PROVIDER = "provider1";
@Rule
public final DbTester db = DbTester.create();
@Test
public void selectByIdentityProvider_returnOnlyGroupForTheIdentityProvider() {
- List<ExternalGroupDto> expectedGroups = createAndInsertExternalGroupDtos("provider1", 3);
+ List<ExternalGroupDto> expectedGroups = createAndInsertExternalGroupDtos(PROVIDER, 3);
createAndInsertExternalGroupDtos("provider2", 1);
- List<ExternalGroupDto> savedGroup = underTest.selectByIdentityProvider(dbSession, "provider1");
+ List<ExternalGroupDto> savedGroup = underTest.selectByIdentityProvider(dbSession, PROVIDER);
assertThat(savedGroup).containsExactlyInAnyOrderElementsOf(expectedGroups);
}
@Test
public void deleteByGroupUuid_deletesTheGroup() {
- List<ExternalGroupDto> insertedGroups = createAndInsertExternalGroupDtos("provider1", 3);
+ List<ExternalGroupDto> insertedGroups = createAndInsertExternalGroupDtos(PROVIDER, 3);
ExternalGroupDto toRemove = insertedGroups.remove(0);
underTest.deleteByGroupUuid(dbSession, toRemove.groupUuid());
- List<ExternalGroupDto> remainingGroups = underTest.selectByIdentityProvider(dbSession, "provider1");
+ List<ExternalGroupDto> remainingGroups = underTest.selectByIdentityProvider(dbSession, PROVIDER);
assertThat(remainingGroups).containsExactlyInAnyOrderElementsOf(insertedGroups);
}
+ @Test
+ public void deleteByExternalIdentityProvider_onlyDeletesGroupOfTheRequestedProvider() {
+ createAndInsertExternalGroupDtos(PROVIDER, 3);
+ List<ExternalGroupDto> groupsProvider2 = createAndInsertExternalGroupDtos("provider2", 3);
+
+ underTest.deleteByExternalIdentityProvider(dbSession, PROVIDER);
+
+ assertThat(underTest.selectByIdentityProvider(dbSession, PROVIDER)).isEmpty();
+ assertThat(underTest.selectByIdentityProvider(dbSession, "provider2")).containsExactlyInAnyOrderElementsOf(groupsProvider2);
+ }
+
@Test
public void getManagedGroupsSqlFilter_whenFilterByManagedIsTrue_returnsCorrectQuery() {
String filterManagedUser = underTest.getManagedGroupSqlFilter(true);
public String getManagedGroupSqlFilter(boolean filterByManaged) {
return String.format("%s exists (select group_uuid from external_groups eg where eg.group_uuid = uuid)", filterByManaged ? "" : "not");
}
+
+ public void deleteByExternalIdentityProvider(DbSession dbSession, String externalIdentityProvider) {
+ mapper(dbSession).deleteByExternalIdentityProvider(externalIdentityProvider);
+ }
}
Optional<ExternalGroupDto> selectByExternalIdAndIdentityProvider(@Param("externalId") String externalId, @Param("identityProvider") String identityProvider);
void deleteByGroupUuid(@Param("groupUuid") String groupUuid);
+
+ void deleteByExternalIdentityProvider(String externalIdentityProvider);
}
delete from external_groups where group_uuid = #{groupUuid, jdbcType=VARCHAR}
</delete>
+ <delete id="deleteByExternalIdentityProvider" parameterType="String">
+ delete from external_groups where external_identity_provider = #{externalIdentityProvider, jdbcType=VARCHAR}
+ </delete>
+
</mapper>