Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop' into develop-mpa-8170-Update-Implement-Tests-F…
Browse files Browse the repository at this point in the history
…or-Org-Service
  • Loading branch information
Pavlo Petrina authored May 16, 2019
2 parents 95504ec + 5d2da32 commit b7febb5
Show file tree
Hide file tree
Showing 19 changed files with 3,626 additions and 90 deletions.
3,482 changes: 3,482 additions & 0 deletions ApiDocs/_apidoc.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>scalecube-organization-parent</artifactId>
<version>2.1.3-SNAPSHOT</version>
<version>2.1.5-SNAPSHOT</version>

<packaging>pom</packaging>

Expand All @@ -25,7 +25,7 @@

<properties>
<scalecube.config.version>0.3.9</scalecube.config.version>
<scalecube-services.version>2.5.4</scalecube-services.version>
<scalecube-services.version>2.5.9</scalecube-services.version>
<scalecube-security-jwt.version>1.0.7</scalecube-security-jwt.version>
<junit-jupiter.version>5.3.2</junit-jupiter.version>
<jackson.version>2.9.8</jackson.version>
Expand Down
2 changes: 1 addition & 1 deletion scalecube-organization-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-organization-parent</artifactId>
<version>2.1.3-SNAPSHOT</version>
<version>2.1.5-SNAPSHOT</version>
</parent>

<artifactId>scalecube-organization-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

public class ApiKey {

protected String keyId;
protected String name;
protected Map<String, String> claims;
protected String key;

public String keyId() {
return this.keyId;
}

public String name() {
return this.name;
}
Expand All @@ -31,6 +36,9 @@ public boolean equals(Object o) {

ApiKey apiKey = (ApiKey) o;

if (keyId != null ? !keyId.equals(apiKey.keyId) : apiKey.keyId != null) {
return false;
}
if (name != null ? !name.equals(apiKey.name) : apiKey.name != null) {
return false;
}
Expand All @@ -42,7 +50,8 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
int result = keyId != null ? keyId.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (claims != null ? claims.hashCode() : 0);
result = 31 * result + (key != null ? key.hashCode() : 0);
return result;
Expand Down
2 changes: 1 addition & 1 deletion scalecube-organization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-organization-parent</artifactId>
<version>2.1.3-SNAPSHOT</version>
<version>2.1.5-SNAPSHOT</version>
</parent>

<artifactId>scalecube-organization</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public Mono<CreateOrganizationResponse> createOrganization(CreateOrganizationReq
CreateOrganization.builder()
.tokenVerifier(tokenVerifier)
.repository(repository)
.keyPairGenerator(keyPairGenerator)
.keyStore(keyStore)
.build()
.execute(request);

Expand Down Expand Up @@ -134,6 +132,7 @@ public Mono<DeleteOrganizationResponse> deleteOrganization(DeleteOrganizationReq
DeleteOrganization.builder()
.tokenVerifier(tokenVerifier)
.repository(repository)
.keyStore(keyStore)
.build()
.execute(request);
logger.debug("deleteOrganization: exit, request: {}, response: {}", request, response);
Expand Down Expand Up @@ -266,6 +265,7 @@ public Mono<GetOrganizationResponse> addOrganizationApiKey(AddOrganizationApiKey
AddOrganizationApiKey.builder()
.tokenVerifier(tokenVerifier)
.repository(repository)
.keyPairGenerator(keyPairGenerator)
.keyStore(keyStore)
.build()
.execute(request);
Expand All @@ -291,6 +291,7 @@ public Mono<GetOrganizationResponse> deleteOrganizationApiKey(
DeleteOrganizationApiKey.builder()
.tokenVerifier(tokenVerifier)
.repository(repository)
.keyStore(keyStore)
.build()
.execute(request);
logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class Organization extends Entity {

private String name;
private String email;
private String keyId;
private Set<OrganizationMember> members = new HashSet<>();
private Set<ApiKey> apiKeys = new HashSet<>();

Expand All @@ -27,14 +26,12 @@ public class Organization extends Entity {
* @param id organization id.
* @param name organization name.
* @param email organization email.
* @param keyId organization key id.
* @param creatorUserId user id of organization creator.
*/
public Organization(String id, String name, String email, String keyId, String creatorUserId) {
public Organization(String id, String name, String email, String creatorUserId) {
this.id = requireNonNull(id, "organization id cannot be null");
this.name = requireNonNull(name, "organization name cannot be null");
this.email = requireNonNull(email, "organization email cannot be null");
this.keyId = requireNonNull(keyId, "organization keyId cannot be null");

addMember(
new OrganizationMember(
Expand All @@ -50,10 +47,6 @@ public String email() {
return email;
}

public String keyId() {
return keyId;
}

public Set<OrganizationMember> members() {
return Collections.unmodifiableSet(members);
}
Expand Down Expand Up @@ -101,7 +94,6 @@ public String toString() {
.add("id='" + id + "'")
.add("name='" + name + "'")
.add("email='" + email + "'")
.add("keyId='" + keyId + "'")
.add("members=" + members)
.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
import io.scalecube.organization.tokens.TokenVerifier;
import io.scalecube.organization.tokens.store.ApiKeyBuilder;
import io.scalecube.organization.tokens.store.KeyStore;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.EnumSet;
import java.util.UUID;

public class AddOrganizationApiKey
extends ServiceOperation<AddOrganizationApiKeyRequest, GetOrganizationResponse> {

private final KeyPairGenerator keyPairGenerator;
private final KeyStore keyStore;

private AddOrganizationApiKey(Builder builder) {
super(builder.tokenVerifier, builder.repository);
this.keyPairGenerator = builder.keyPairGenerator;
this.keyStore = builder.keyStore;
}

Expand All @@ -47,16 +52,15 @@ protected GetOrganizationResponse process(
throw new AccessPermissionException(
String.format(
"user: '%s', name: '%s', role: '%s' cannot add api key with higher role '%s'",
context.profile().userId(),
context.profile().name(),
callerRole,
targetRole));
context.profile().userId(), context.profile().name(), callerRole, targetRole));
}
}
}

ApiKey apiKey = ApiKeyBuilder.build(keyStore, organization, request);
String keyId = UUID.randomUUID().toString();

KeyPair keyPair = generateKeyPair(keyId);
ApiKey apiKey = ApiKeyBuilder.build(keyPair.getPrivate(), organization.id(), keyId, request);
organization.addApiKey(apiKey);

context.repository().save(organization.id(), organization);
Expand All @@ -81,6 +85,12 @@ protected void validate(AddOrganizationApiKeyRequest request, OperationServiceCo
}
}

private KeyPair generateKeyPair(String keyId) {
KeyPair keyPair = keyPairGenerator.generateKeyPair();
keyStore.store(keyId, keyPair);
return keyPair;
}

@Override
protected Token getToken(AddOrganizationApiKeyRequest request) {
return request.token();
Expand All @@ -93,6 +103,7 @@ public static Builder builder() {
public static class Builder {
private TokenVerifier tokenVerifier;
private OrganizationsRepository repository;
private KeyPairGenerator keyPairGenerator;
private KeyStore keyStore;

public Builder tokenVerifier(TokenVerifier tokenVerifier) {
Expand All @@ -105,6 +116,11 @@ public Builder repository(OrganizationsRepository repository) {
return this;
}

public Builder keyPairGenerator(KeyPairGenerator keyPairGenerator) {
this.keyPairGenerator = keyPairGenerator;
return this;
}

public Builder keyStore(KeyStore keyStore) {
this.keyStore = keyStore;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@
import io.scalecube.organization.repository.OrganizationsRepository;
import io.scalecube.organization.tokens.IdGenerator;
import io.scalecube.organization.tokens.TokenVerifier;
import io.scalecube.organization.tokens.store.KeyStore;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.UUID;

public final class CreateOrganization
extends OrganizationInfoOperation<CreateOrganizationRequest, CreateOrganizationResponse> {

private final KeyPairGenerator keyPairGenerator;
private final KeyStore keyStore;

private CreateOrganization(Builder builder) {
super(builder.tokenVerifier, builder.repository);
this.keyPairGenerator = builder.keyPairGenerator;
this.keyStore = builder.keyStore;
}

public static Builder builder() {
Expand All @@ -40,14 +31,6 @@ protected CreateOrganizationResponse process(

Organization organization = createOrganization(request, context, id);

try {
generateOrganizationKeyPair(organization);
} catch (Exception ex) {
// failed to persist organization secret rollback
context.repository().deleteById(organization.id());
throw ex;
}

return new CreateOrganizationResponse(
OrganizationInfo.builder()
.id(organization.id())
Expand All @@ -63,17 +46,11 @@ private Organization createOrganization(
id,
request.name(),
request.email(),
UUID.randomUUID().toString(),
context.profile().userId());

return context.repository().save(organization.id(), organization);
}

private void generateOrganizationKeyPair(Organization organization) {
KeyPair keyPair = keyPairGenerator.generateKeyPair();
keyStore.store(organization.keyId(), keyPair);
}

@Override
protected Token getToken(CreateOrganizationRequest request) {
return request.token();
Expand All @@ -82,8 +59,6 @@ protected Token getToken(CreateOrganizationRequest request) {
public static class Builder {
private TokenVerifier tokenVerifier;
private OrganizationsRepository repository;
private KeyPairGenerator keyPairGenerator;
private KeyStore keyStore;

public Builder tokenVerifier(TokenVerifier tokenVerifier) {
this.tokenVerifier = tokenVerifier;
Expand All @@ -95,16 +70,6 @@ public Builder repository(OrganizationsRepository repository) {
return this;
}

public Builder keyPairGenerator(KeyPairGenerator keyPairGenerator) {
this.keyPairGenerator = keyPairGenerator;
return this;
}

public Builder keyStore(KeyStore keyStore) {
this.keyStore = keyStore;
return this;
}

public CreateOrganization build() {
return new CreateOrganization(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import io.scalecube.organization.domain.Organization;
import io.scalecube.organization.repository.OrganizationsRepository;
import io.scalecube.organization.tokens.TokenVerifier;
import io.scalecube.organization.tokens.store.KeyStore;

public class DeleteOrganization
extends ServiceOperation<DeleteOrganizationRequest, DeleteOrganizationResponse> {
private final KeyStore keyStore;

private DeleteOrganization(TokenVerifier tokenVerifier, OrganizationsRepository repository) {
private DeleteOrganization(
TokenVerifier tokenVerifier, OrganizationsRepository repository, KeyStore keyStore) {
super(tokenVerifier, repository);
this.keyStore = keyStore;
}

@Override
Expand All @@ -20,6 +24,10 @@ protected DeleteOrganizationResponse process(
Organization organization = getOrganization(request.organizationId());
checkOwnerAccess(organization, context.profile());
context.repository().deleteById(organization.id());
organization.apiKeys().stream()
.map(apiKey -> apiKey.keyId())
.filter(keyId -> keyId != null && !keyId.isEmpty())
.forEach(keyId -> keyStore.delete(keyId));
return new DeleteOrganizationResponse(organization.id(), true);
}

Expand All @@ -42,6 +50,7 @@ public static Builder builder() {
public static class Builder {
private TokenVerifier tokenVerifier;
private OrganizationsRepository repository;
private KeyStore keyStore;

public Builder tokenVerifier(TokenVerifier tokenVerifier) {
this.tokenVerifier = tokenVerifier;
Expand All @@ -54,7 +63,12 @@ public Builder repository(OrganizationsRepository repository) {
}

public DeleteOrganization build() {
return new DeleteOrganization(tokenVerifier, repository);
return new DeleteOrganization(tokenVerifier, repository, keyStore);
}

public Builder keyStore(KeyStore keyStore) {
this.keyStore = keyStore;
return this;
}
}
}
Loading

0 comments on commit b7febb5

Please sign in to comment.