Commit 96d0fb2e authored by Greg Messner's avatar Greg Messner
Browse files

Mods to support sudo (#92).

parent 1d025522
...@@ -6,6 +6,7 @@ import javax.ws.rs.core.Response; ...@@ -6,6 +6,7 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.Constants.TokenType; import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.models.Session; import org.gitlab4j.api.models.Session;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Version; import org.gitlab4j.api.models.Version;
/** /**
...@@ -48,6 +49,7 @@ public class GitLabApi { ...@@ -48,6 +49,7 @@ public class GitLabApi {
private Session session; private Session session;
/** /**
* Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance * Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
* using returned private token and the specified GitLab API version. * using returned private token and the specified GitLab API version.
...@@ -288,6 +290,70 @@ public class GitLabApi { ...@@ -288,6 +290,70 @@ public class GitLabApi {
userApi = new UserApi(this); userApi = new UserApi(this);
} }
/**
* Sets up all future calls to the GitLab API to be done as another user specified by sudoAsUsername.
* To revert back to normal non-sudo operation you must call unsudo(), or pass null as the username.
*
* @param sudoAsUsername the username to sudo as, null will turn off sudo
* @throws GitLabApiException if any exception occurs
*/
public void sudo(String sudoAsUsername) throws GitLabApiException {
if (sudoAsUsername == null || sudoAsUsername.trim().length() == 0) {
apiClient.setSudoAsId(null);
return;
}
// Get the User specified by username, if you are not an admin or the username is not found, this will fail
User user = getUserApi().getUser(sudoAsUsername);
if (user == null || user.getId() == null) {
throw new GitLabApiException("the specified username was not found");
}
Integer sudoAsId = user.getId();
apiClient.setSudoAsId(sudoAsId);
}
/**
* Turns off the currently configured sudo as ID.
*/
public void unsudo() {
apiClient.setSudoAsId(null);
}
/**
* Sets up all future calls to the GitLab API to be done as another user specified by provided user ID.
* To revert back to normal non-sudo operation you must call unsudo(), or pass null as the sudoAsId.
*
* @param sudoAsId the ID of the user to sudo as, null will turn off sudo
* @throws GitLabApiException if any exception occurs
*/
public void setSudoAsId(Integer sudoAsId) throws GitLabApiException {
if (sudoAsId == null) {
apiClient.setSudoAsId(null);
return;
}
// Get the User specified by the sudoAsId, if you are not an admin or the username is not found, this will fail
User user = getUserApi().getUser(sudoAsId);
if (user == null || user.getId() != sudoAsId) {
throw new GitLabApiException("the specified user ID was not found");
}
apiClient.setSudoAsId(sudoAsId);
}
/**
* Get the current sudo as ID, will return null if not in sudo mode.
*
* @return the current sudo as ID, will return null if not in sudo mode
*/
public Integer getSudoAsId() {
return (this.apiClient.getSudoAsId());
}
/** /**
* Return the GitLab API version that this instance is using. * Return the GitLab API version that this instance is using.
* *
......
...@@ -38,6 +38,7 @@ import org.glassfish.jersey.client.ClientProperties; ...@@ -38,6 +38,7 @@ import org.glassfish.jersey.client.ClientProperties;
public class GitLabApiClient { public class GitLabApiClient {
protected static final String PRIVATE_TOKEN_HEADER = "PRIVATE-TOKEN"; protected static final String PRIVATE_TOKEN_HEADER = "PRIVATE-TOKEN";
protected static final String SUDO_HEADER = "Sudo";
protected static final String AUTHORIZATION_HEADER = "Authorization"; protected static final String AUTHORIZATION_HEADER = "Authorization";
protected static final String X_GITLAB_TOKEN_HEADER = "X-Gitlab-Token"; protected static final String X_GITLAB_TOKEN_HEADER = "X-Gitlab-Token";
...@@ -50,6 +51,7 @@ public class GitLabApiClient { ...@@ -50,6 +51,7 @@ public class GitLabApiClient {
private boolean ignoreCertificateErrors; private boolean ignoreCertificateErrors;
private SSLContext openSslContext; private SSLContext openSslContext;
private HostnameVerifier openHostnameVerifier; private HostnameVerifier openHostnameVerifier;
private Integer sudoAsId;
/** /**
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version, * Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
...@@ -211,6 +213,24 @@ public class GitLabApiClient { ...@@ -211,6 +213,24 @@ public class GitLabApiClient {
clientConfig.register(JacksonJson.class); clientConfig.register(JacksonJson.class);
} }
/**
* Set the ID of the user to sudo as.
*
* @param sudoAsId the ID of the user to sudo as
*/
Integer getSudoAsId() {
return (sudoAsId);
}
/**
* Set the ID of the user to sudo as.
*
* @param sudoAsId the ID of the user to sudo as
*/
void setSudoAsId(Integer sudoAsId) {
this.sudoAsId = sudoAsId;
}
/** /**
* Construct a REST URL with the specified path arguments. * Construct a REST URL with the specified path arguments.
* *
...@@ -467,10 +487,18 @@ public class GitLabApiClient { ...@@ -467,10 +487,18 @@ public class GitLabApiClient {
String authHeader = (tokenType == TokenType.ACCESS ? AUTHORIZATION_HEADER : PRIVATE_TOKEN_HEADER); String authHeader = (tokenType == TokenType.ACCESS ? AUTHORIZATION_HEADER : PRIVATE_TOKEN_HEADER);
String authValue = (tokenType == TokenType.ACCESS ? "Bearer " + authToken : authToken); String authValue = (tokenType == TokenType.ACCESS ? "Bearer " + authToken : authToken);
if (accept == null || accept.trim().length() == 0) Invocation.Builder builder = target.request();
return (target.request().header(authHeader, authValue)); if (accept == null || accept.trim().length() == 0) {
else builder = builder.header(authHeader, authValue);
return (target.request().header(authHeader, authValue).accept(accept)); } else {
builder = builder.header(authHeader, authValue).accept(accept);
}
// If sudo as ID is set add the Sudo header
if (sudoAsId != null && sudoAsId.intValue() > 0)
builder = builder.header(SUDO_HEADER, sudoAsId);
return (builder);
} }
/** /**
......
...@@ -2,6 +2,7 @@ package org.gitlab4j.api; ...@@ -2,6 +2,7 @@ package org.gitlab4j.api;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
import java.util.List; import java.util.List;
...@@ -73,21 +74,40 @@ public class TestNamespaceApi { ...@@ -73,21 +74,40 @@ public class TestNamespaceApi {
public void testGetNamespaces() throws GitLabApiException { public void testGetNamespaces() throws GitLabApiException {
List<Namespace> namespaces = gitLabApi.getNamespaceApi().getNamespaces(); List<Namespace> namespaces = gitLabApi.getNamespaceApi().getNamespaces();
assertNotNull(namespaces); assertNotNull(namespaces);
assertEquals(TEST_NAMESPACE, namespaces.get(0).getName()); for (Namespace namespace : namespaces) {
if (TEST_NAMESPACE.equals(namespace.getName()))
return;
}
fail(TEST_NAMESPACE + " not found!");
} }
@Test @Test
public void testGetNamespacesViaPager() throws GitLabApiException { public void testGetNamespacesViaPager() throws GitLabApiException {
Pager<Namespace> pager = gitLabApi.getNamespaceApi().getNamespaces(10); Pager<Namespace> pager = gitLabApi.getNamespaceApi().getNamespaces(10);
assertNotNull(pager); assertNotNull(pager);
assertEquals(TEST_NAMESPACE, pager.next().get(0).getName());
while (pager.hasNext()) {
List<Namespace> namespaces = pager.next();
for (Namespace namespace : namespaces) {
if (TEST_NAMESPACE.equals(namespace.getName()))
return;
}
}
fail(TEST_NAMESPACE + " not found!");
} }
@Test @Test
public void testGetNamespacesByPage() throws GitLabApiException { public void testGetNamespacesByPage() throws GitLabApiException {
List<Namespace> namespaces = gitLabApi.getNamespaceApi().getNamespaces(1, 10); List<Namespace> namespaces = gitLabApi.getNamespaceApi().getNamespaces(1, 10);
assertNotNull(namespaces); assertNotNull(namespaces);
assertEquals(TEST_NAMESPACE, namespaces.get(0).getName()); for (Namespace namespace : namespaces) {
if (TEST_NAMESPACE.equals(namespace.getName()))
return;
}
fail(TEST_NAMESPACE + " not found!");
} }
@Test @Test
......
...@@ -354,7 +354,7 @@ public class TestProjectApi { ...@@ -354,7 +354,7 @@ public class TestProjectApi {
public void testProjectPerPage() throws GitLabApiException { public void testProjectPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 10); List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 10);
assertNotNull(projects); assertNotNull(projects);
assertEquals(10, projects.size()); assertTrue(projects.size() > 0);
} }
@Test @Test
......
...@@ -2,6 +2,7 @@ package org.gitlab4j.api; ...@@ -2,6 +2,7 @@ package org.gitlab4j.api;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
...@@ -27,10 +28,12 @@ public class TestUserApi { ...@@ -27,10 +28,12 @@ public class TestUserApi {
private static final String TEST_HOST_URL; private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
private static final String TEST_USERNAME; private static final String TEST_USERNAME;
private static final String TEST_SUDO_AS_USERNAME;
static { static {
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME"); TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME");
} }
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
...@@ -89,4 +92,36 @@ public class TestUserApi { ...@@ -89,4 +92,36 @@ public class TestUserApi {
assertNotNull(user); assertNotNull(user);
assertEquals(TEST_USERNAME, user.getUsername()); assertEquals(TEST_USERNAME, user.getUsername());
} }
@Test
public void testSudoAsUser() throws GitLabApiException {
assumeTrue(TEST_SUDO_AS_USERNAME != null);
try {
gitLabApi.sudo(TEST_SUDO_AS_USERNAME);
User user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
assertEquals(TEST_SUDO_AS_USERNAME, user.getUsername());
Integer sudoAsId = user.getId();
gitLabApi.sudo(null);
user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
assertEquals(TEST_USERNAME, user.getUsername());
gitLabApi.unsudo();
assertNull(gitLabApi.getSudoAsId());
gitLabApi.setSudoAsId(sudoAsId);
user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
assertEquals(sudoAsId, user.getId());
assertEquals(TEST_SUDO_AS_USERNAME, user.getUsername());
} finally {
gitLabApi.unsudo();
}
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment