use of io.dockstore.webservice.jdbi.TokenDAO in project dockstore by dockstore.
the class UserResourceServicesIT method setup.
@Before
public void setup() throws Exception {
CommonTestUtilities.dropAndCreateWithTestData(SUPPORT, false, DROPWIZARD_CONFIGURATION_FILE_PATH);
DockstoreWebserviceApplication application = SUPPORT.getApplication();
SessionFactory sessionFactory = application.getHibernate().getSessionFactory();
TokenDAO tokenDAO = new TokenDAO(sessionFactory);
// non-confidential test database sequences seem messed up and need to be iterated past, but other tests may depend on ids
testingPostgres.runUpdateStatement("alter sequence enduser_id_seq increment by 50 restart with 100");
testingPostgres.runUpdateStatement("alter sequence token_id_seq increment by 50 restart with 100");
// used to allow us to use tokenDAO outside of the web service
Session session = application.getHibernate().getSessionFactory().openSession();
ManagedSessionContext.bind(session);
final Transaction transaction = session.beginTransaction();
tokenDAO.create(createToken(SUFFIX1, GITHUB_ACCOUNT_USERNAME_1, GITHUB_USER1_ID));
tokenDAO.create(createToken(SUFFIX2, GITHUB_ACCOUNT_USERNAME_2, GITHUB_USER2_ID));
transaction.commit();
session.close();
}
use of io.dockstore.webservice.jdbi.TokenDAO in project dockstore by dockstore.
the class UserResource method addUserToDockstoreWorkflows.
@PATCH
@Timed
@UnitOfWork
@Path("/{userId}/workflows")
@ApiOperation(value = "Adds a user to any Dockstore workflows that they should have access to.", authorizations = { @Authorization(value = JWT_SECURITY_DEFINITION_NAME) }, response = Workflow.class, responseContainer = "List")
@Operation(operationId = "addUserToDockstoreWorkflows", description = "Adds the logged-in user to any Dockstore workflows that they should have access to.", security = @SecurityRequirement(name = OPENAPI_JWT_SECURITY_DEFINITION_NAME))
public List<Workflow> addUserToDockstoreWorkflows(@ApiParam(hidden = true) @Parameter(hidden = true, name = "user") @Auth User authUser, @ApiParam(name = "userId", required = true, value = "User to update") @PathParam("userId") @Parameter(name = "userId", in = ParameterIn.PATH, description = "User to update", required = true) long userId, @ApiParam(name = "emptyBody", value = APPEASE_SWAGGER_PATCH) @Parameter(description = APPEASE_SWAGGER_PATCH, name = "emptyBody") String emptyBody) {
final User user = userDAO.findById(authUser.getId());
if (user == null || !Objects.equals(userId, user.getId())) {
throw new CustomWebApplicationException("The user id provided does not match the logged-in user id.", HttpStatus.SC_BAD_REQUEST);
}
// Ignore hosted workflows
List<SourceControl> sourceControls = Arrays.stream(SourceControl.values()).filter(sourceControl -> !Objects.equals(sourceControl, SourceControl.DOCKSTORE)).collect(Collectors.toList());
List<Token> scTokens = getAndRefreshTokens(user, tokenDAO, client, bitbucketClientID, bitbucketClientSecret).stream().filter(token -> sourceControls.contains(token.getTokenSource().getSourceControl())).collect(Collectors.toList());
scTokens.forEach(token -> {
SourceCodeRepoInterface sourceCodeRepo = SourceCodeRepoFactory.createSourceCodeRepo(token);
Map<String, String> gitUrlToRepositoryId = sourceCodeRepo.getWorkflowGitUrl2RepositoryId();
Set<String> organizations = gitUrlToRepositoryId.values().stream().map(repository -> repository.split("/")[0]).collect(Collectors.toSet());
organizations.forEach(organization -> {
List<Workflow> workflowsWithoutuser = workflowDAO.findByOrganizationWithoutUser(token.getTokenSource().getSourceControl(), organization, user);
workflowsWithoutuser.forEach(workflow -> workflow.addUser(user));
});
});
return convertMyWorkflowsToWorkflow(this.bioWorkflowDAO.findUserBioWorkflows(user.getId()));
}
use of io.dockstore.webservice.jdbi.TokenDAO in project dockstore by dockstore.
the class UserResource method getGitRepositoryMap.
/**
* For a given user and git registry, retrieve a map of git url to repository path
* @param user
* @param gitRegistry
* @return mapping of git url to repository path
*/
private Map<String, String> getGitRepositoryMap(User user, SourceControl gitRegistry) {
List<Token> scTokens = getAndRefreshTokens(user, tokenDAO, client, bitbucketClientID, bitbucketClientSecret).stream().filter(token -> Objects.equals(token.getTokenSource().getSourceControl(), gitRegistry)).collect(Collectors.toList());
if (scTokens.size() > 0) {
Token scToken = scTokens.get(0);
SourceCodeRepoInterface sourceCodeRepo = SourceCodeRepoFactory.createSourceCodeRepo(scToken);
return sourceCodeRepo.getWorkflowGitUrl2RepositoryId();
} else {
return new HashMap<>();
}
}
use of io.dockstore.webservice.jdbi.TokenDAO in project dockstore by dockstore.
the class SamPermissionsImplTest method setup.
@Before
public void setup() {
ownerPolicy = new AccessPolicyResponseEntry();
ownerPolicy.setPolicyName(SamConstants.OWNER_POLICY);
AccessPolicyMembership accessPolicyMembership = new AccessPolicyMembership();
accessPolicyMembership.setRoles(Collections.singletonList(SamConstants.OWNER_POLICY));
accessPolicyMembership.setMemberEmails(Collections.singletonList("jdoe@ucsc.edu"));
ownerPolicy.setPolicy(accessPolicyMembership);
writerPolicy = new AccessPolicyResponseEntry();
writerPolicy.setPolicyName(SamConstants.WRITE_POLICY);
AccessPolicyMembership writerMembership = new AccessPolicyMembership();
writerMembership.setRoles(Collections.singletonList(SamConstants.WRITE_POLICY));
writerMembership.setMemberEmails(Collections.singletonList(JANE_DOE_GMAIL_COM));
writerPolicy.setPolicy(writerMembership);
AccessPolicyResponseEntry readerPolicy = new AccessPolicyResponseEntry();
readerPolicy.setPolicyName(SamConstants.READ_POLICY);
AccessPolicyMembership readerMembership = new AccessPolicyMembership();
readerMembership.setRoles(Collections.singletonList(SamConstants.READ_POLICY));
readerMembership.setMemberEmails(Collections.singletonList(JANE_DOE_GMAIL_COM));
readerPolicy.setPolicy(readerMembership);
TokenDAO tokenDAO = Mockito.mock(TokenDAO.class);
DockstoreWebserviceConfiguration configMock = Mockito.mock(DockstoreWebserviceConfiguration.class);
when(configMock.getSamConfiguration()).thenReturn(new DockstoreWebserviceConfiguration.SamConfiguration());
samPermissionsImpl = Mockito.spy(new SamPermissionsImpl(tokenDAO, configMock));
doReturn(Optional.of("my token")).when(samPermissionsImpl).googleAccessToken(userMock);
doReturn(Mockito.mock(Token.class)).when(samPermissionsImpl).googleToken(userMock);
resourcesApiMock = Mockito.mock(ResourcesApi.class);
ApiClient apiClient = Mockito.mock(ApiClient.class);
when(apiClient.escapeString(ArgumentMatchers.anyString())).thenAnswer(invocation -> invocation.getArgument(0));
when(resourcesApiMock.getApiClient()).thenReturn(apiClient);
when(samPermissionsImpl.getResourcesApi(userMock)).thenReturn(resourcesApiMock);
workflowInstance = Mockito.mock(Workflow.class);
when(workflowInstance.getWorkflowPath()).thenReturn("foo");
ownerPermission = new Permission();
ownerPermission.setEmail("jdoe@ucsc.edu");
ownerPermission.setRole(Role.OWNER);
Assert.assertThat(samPermissionsImpl.accessPolicyResponseEntryToUserPermissions(Collections.singletonList(ownerPolicy)), CoreMatchers.is(Collections.singletonList(ownerPermission)));
writerPermission = new Permission();
writerPermission.setEmail(JANE_DOE_GMAIL_COM);
writerPermission.setRole(Role.WRITER);
readerPermission = new Permission();
readerPermission.setEmail(JANE_DOE_GMAIL_COM);
readerPermission.setRole(Role.READER);
AccessPolicyMembership readerAccessPolicyMembership = new AccessPolicyMembership();
readerAccessPolicyResponseEntry = new AccessPolicyResponseEntry();
readerAccessPolicyResponseEntry.setPolicy(readerAccessPolicyMembership);
readerAccessPolicyResponseEntry.setPolicyName(SamConstants.READ_POLICY);
readerAccessPolicyResponseEntry.getPolicy().addRolesItem(SamConstants.READ_POLICY);
readerAccessPolicyResponseEntry.getPolicy().addMemberEmailsItem(JANE_DOE_GMAIL_COM);
final User.Profile profile = new User.Profile();
profile.email = JANE_DOE_GMAIL_COM;
final Map<String, User.Profile> map = new HashMap<>();
map.put(TokenType.GOOGLE_COM.toString(), profile);
when(userMock.getUserProfiles()).thenReturn(map);
}
use of io.dockstore.webservice.jdbi.TokenDAO in project dockstore by dockstore.
the class AdvancedIndexingBenchmarkIT method testCreate10000Tools.
@Test
public void testCreate10000Tools() {
this.sessionFactory = application.getHibernate().getSessionFactory();
try {
Transaction transaction = this.sessionFactory.openSession().getTransaction();
transaction.begin();
TokenDAO tokenDAO = new TokenDAO(sessionFactory);
User user = new User();
user.setIsAdmin(true);
user.setUsername("travistest");
Token token = new Token();
token.setUserId(1);
token.setUsername("travistest");
token.setContent("iamafakedockstoretoken");
token.setTokenSource(TokenType.DOCKSTORE);
tokenDAO.create(token);
Token token2 = new Token();
token2.setUserId(1);
token2.setUsername("travistest");
token2.setContent("iamafakegithubtoken");
token2.setTokenSource(TokenType.GITHUB_COM);
tokenDAO.create(token2);
UserDAO userDAO = new UserDAO(sessionFactory);
userDAO.create(user);
transaction.commit();
} finally {
session.close();
}
session = application.getHibernate().getSessionFactory().openSession();
ManagedSessionContext.bind(session);
for (int i = 0; i < TOOL_COUNT; i++) {
createTool();
}
Response response = client.target("http://localhost:" + SUPPORT.getLocalPort() + "/containers/published").request().get();
List<Tool> tools = response.readEntity(new GenericType<List<Tool>>() {
});
int actualToolCount = tools.size();
assertEquals("Supposed to have " + TOOL_COUNT + " tools. Instead got " + actualToolCount + " tools.", actualToolCount, TOOL_COUNT);
LOGGER.error("Amount of tools created: " + String.valueOf(actualToolCount));
for (Long indexTime : indexTimes) {
LOGGER.error(String.valueOf(indexTime));
}
}
Aggregations