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;
import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.models.Session;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Version;
/**
......@@ -48,6 +49,7 @@ public class GitLabApi {
private Session session;
/**
* 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.
......@@ -288,6 +290,70 @@ public class GitLabApi {
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.
*
......
......@@ -38,6 +38,7 @@ import org.glassfish.jersey.client.ClientProperties;
public class GitLabApiClient {
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 X_GITLAB_TOKEN_HEADER = "X-Gitlab-Token";
......@@ -50,6 +51,7 @@ public class GitLabApiClient {
private boolean ignoreCertificateErrors;
private SSLContext openSslContext;
private HostnameVerifier openHostnameVerifier;
private Integer sudoAsId;
/**
* Construct an instance to communicate with a GitLab API server using the specified GitLab API version,
......@@ -211,6 +213,24 @@ public class GitLabApiClient {
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.
*
......@@ -467,10 +487,18 @@ public class GitLabApiClient {
String authHeader = (tokenType == TokenType.ACCESS ? AUTHORIZATION_HEADER : PRIVATE_TOKEN_HEADER);
String authValue = (tokenType == TokenType.ACCESS ? "Bearer " + authToken : authToken);
if (accept == null || accept.trim().length() == 0)
return (target.request().header(authHeader, authValue));
else
return (target.request().header(authHeader, authValue).accept(accept));
Invocation.Builder builder = target.request();
if (accept == null || accept.trim().length() == 0) {
builder = builder.header(authHeader, authValue);
} 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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.util.List;
......@@ -73,21 +74,40 @@ public class TestNamespaceApi {
public void testGetNamespaces() throws GitLabApiException {
List<Namespace> namespaces = gitLabApi.getNamespaceApi().getNamespaces();
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
public void testGetNamespacesViaPager() throws GitLabApiException {
Pager<Namespace> pager = gitLabApi.getNamespaceApi().getNamespaces(10);
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
public void testGetNamespacesByPage() throws GitLabApiException {
List<Namespace> namespaces = gitLabApi.getNamespaceApi().getNamespaces(1, 10);
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
......
......@@ -354,7 +354,7 @@ public class TestProjectApi {
public void testProjectPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 10);
assertNotNull(projects);
assertEquals(10, projects.size());
assertTrue(projects.size() > 0);
}
@Test
......
......@@ -2,6 +2,7 @@ package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assume.assumeTrue;
import org.gitlab4j.api.GitLabApi.ApiVersion;
......@@ -27,10 +28,12 @@ public class TestUserApi {
private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
private static final String TEST_USERNAME;
private static final String TEST_SUDO_AS_USERNAME;
static {
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME");
}
private static GitLabApi gitLabApi;
......@@ -89,4 +92,36 @@ public class TestUserApi {
assertNotNull(user);
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