use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class AuthCredentialsOperationProcessingChain method handlePatchPostPut.
protected static void handlePatchPostPut(Service service, Operation op) {
AuthCredentialsServiceState body = op.getBody(AuthCredentialsServiceState.class);
// Credentials with SYSTEM scope need the password in plain text or they can't be used to
// login into Xenon!
boolean isSystemScope = (body.customProperties != null) && CredentialsScope.SYSTEM.toString().equals(body.customProperties.get(CUSTOM_PROP_CREDENTIALS_SCOPE));
if (!isSystemScope) {
body.privateKey = EncryptionUtils.encrypt(body.privateKey);
op.setBodyNoCloning(body);
}
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class AzureInstanceService method getStorageKeys.
/**
* Gets the storage keys from azure and patches the credential state.
*/
private void getStorageKeys(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
if (ctx.reuseExistingStorageAccount() || ctx.useManagedDisks()) {
// no need to get keys as no new storage description was created
handleAllocation(ctx, nextStage);
return;
}
StorageManagementClientImpl client = getStorageManagementClientImpl(ctx);
client.storageAccounts().listKeysAsync(ctx.storageAccountRGName, ctx.storageAccountName, new AzureAsyncCallback<StorageAccountListKeysResultInner>() {
@Override
public void onError(Throwable e) {
handleError(ctx, e);
}
@Override
public void onSuccess(StorageAccountListKeysResultInner result) {
logFine(() -> String.format("Retrieved the storage account keys for storage" + " account [%s]", ctx.storageAccountName));
AuthCredentialsServiceState storageAuth = new AuthCredentialsServiceState();
storageAuth.customProperties = new HashMap<>();
for (StorageAccountKey key : result.keys()) {
storageAuth.customProperties.put(getStorageAccountKeyName(storageAuth.customProperties), key.value());
}
storageAuth.tenantLinks = ctx.parent.tenantLinks;
Operation patchStorageDescriptionWithKeys = Operation.createPost(createInventoryUri(getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(storageAuth).setCompletion((o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
AuthCredentialsServiceState resultAuth = o.getBody(AuthCredentialsServiceState.class);
ctx.storageDescription.authCredentialsLink = resultAuth.documentSelfLink;
Operation patch = Operation.createPatch(UriUtils.buildUri(getHost(), ctx.storageDescription.documentSelfLink)).setBody(ctx.storageDescription).setCompletion(((completedOp, failure) -> {
if (failure != null) {
handleError(ctx, failure);
return;
}
logFine(() -> "Patched storage description.");
handleAllocation(ctx, nextStage);
}));
sendRequest(patch);
});
sendRequest(patchStorageDescriptionWithKeys);
}
});
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class EndpointAllocationTaskService method createEndpoint.
private void createEndpoint(EndpointAllocationTaskState currentState) {
List<String> createdDocumentLinks = new ArrayList<>();
EndpointState es = currentState.endpointState;
Map<String, String> endpointProperties = currentState.endpointState.endpointProperties;
es.endpointProperties = null;
if (es.documentSelfLink == null) {
es.documentSelfLink = UriUtils.buildUriPath(EndpointService.FACTORY_LINK, this.getHost().nextUUID());
}
// merge endpoint and task tenant links
if (es.tenantLinks == null || es.tenantLinks.isEmpty()) {
es.tenantLinks = currentState.tenantLinks;
} else if (currentState.tenantLinks != null) {
currentState.tenantLinks.forEach(tl -> {
if (!es.tenantLinks.contains(tl)) {
es.tenantLinks.add(tl);
}
});
}
Operation endpointOp = Operation.createPost(this, EndpointService.FACTORY_LINK);
ComputeDescription computeDescription = configureDescription(currentState, es);
ComputeState computeState = configureCompute(currentState, es, endpointProperties);
Operation cdOp = createComputeDescriptionOp(currentState, computeDescription.documentSelfLink);
Operation compOp = createComputeStateOp(currentState, computeState.documentSelfLink);
// pool link
if (currentState.enumerationRequest != null && currentState.enumerationRequest.resourcePoolLink != null) {
es.resourcePoolLink = currentState.enumerationRequest.resourcePoolLink;
computeState.resourcePoolLink = es.resourcePoolLink;
}
OperationSequence sequence;
if (es.authCredentialsLink == null) {
AuthCredentialsServiceState auth = configureAuth(es);
Operation authOp = Operation.createPost(createInventoryUri(this.getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(auth);
sequence = OperationSequence.create(authOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create auth credentials: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(authOp.getId());
AuthCredentialsServiceState authState = o.getBody(AuthCredentialsServiceState.class);
computeDescription.authCredentialsLink = authState.documentSelfLink;
es.authCredentialsLink = authState.documentSelfLink;
cdOp.setBody(computeDescription);
}).next(cdOp);
} else {
cdOp.setBody(computeDescription);
sequence = OperationSequence.create(cdOp);
}
sequence = sequence.setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute description: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(cdOp.getId());
ComputeDescription desc = o.getBody(ComputeDescription.class);
if (!currentState.accountAlreadyExists) {
createdDocumentLinks.add(desc.documentSelfLink);
}
computeState.descriptionLink = desc.documentSelfLink;
es.computeDescriptionLink = desc.documentSelfLink;
});
// Don't create resource pool, if a resource pool link was passed.
if (es.resourcePoolLink == null) {
Operation poolOp = createResourcePoolOp(es);
sequence = sequence.next(poolOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error creating resource" + " pool: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(poolOp.getId());
ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
createdDocumentLinks.add(poolState.documentSelfLink);
es.resourcePoolLink = poolState.documentSelfLink;
computeState.resourcePoolLink = es.resourcePoolLink;
compOp.setBody(computeState);
});
} else {
Operation getPoolOp = Operation.createGet(this, es.resourcePoolLink);
sequence = sequence.next(getPoolOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error retrieving resource" + " pool: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(getPoolOp.getId());
ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
if (poolState.customProperties != null) {
String endpointLink = poolState.customProperties.get(ENDPOINT_LINK_PROP_NAME);
if (endpointLink != null && endpointLink.equals(es.documentSelfLink)) {
sendFailurePatch(this, currentState, new IllegalStateException("Passed resource pool is associated with a different endpoint."));
return;
}
}
es.resourcePoolLink = poolState.documentSelfLink;
computeState.resourcePoolLink = es.resourcePoolLink;
compOp.setBody(computeState);
});
}
sequence.next(compOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute state: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation csOp = ops.get(compOp.getId());
ComputeState c = csOp.getBody(ComputeState.class);
if (!currentState.accountAlreadyExists) {
createdDocumentLinks.add(c.documentSelfLink);
}
es.computeLink = c.documentSelfLink;
endpointOp.setBody(es);
}).next(endpointOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create endpoint state: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation esOp = ops.get(endpointOp.getId());
EndpointState endpoint = esOp.getBody(EndpointState.class);
createdDocumentLinks.add(endpoint.documentSelfLink);
// propagate the endpoint properties to the next stage
endpoint.endpointProperties = endpointProperties;
EndpointAllocationTaskState state = createUpdateSubStageTask(SubStage.INVOKE_ADAPTER);
state.endpointState = endpoint;
state.createdDocumentLinks = createdDocumentLinks;
sendSelfPatch(state);
}).sendWith(this);
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class ResourceGroomerTaskService method populateEndpointLinksByDocumentLinks.
/**
* Helper for creating corresponding object and parsing the response for given documentKind
* to store selfLink and endpointLink.
*/
private static void populateEndpointLinksByDocumentLinks(Map<String, Object> documents, Map<String, Set<String>> endpointLinksByDocumentLinks, Map<String, String> endpointLinkByDocumentLinks) {
for (Object document : documents.values()) {
ServiceDocument doc = Utils.fromJson(document, ServiceDocument.class);
Set<String> endpointLinks = new HashSet<>();
if (doc.documentKind.equals(COMPUTE_STATE_DOCUMENT_KIND)) {
ComputeState state = Utils.fromJson(document, ComputeState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(DISK_STATE_DOCUMENT_KIND)) {
DiskState state = Utils.fromJson(document, DiskState.class);
if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
// skip resources that have never been attached to a particular endpoint
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(COMPUTE_DESCRIPTION_DOCUMENT_KIND)) {
ComputeDescription state = Utils.fromJson(document, ComputeDescription.class);
// only deleting discovered resources
if (!(state.customProperties != null && (ResourceEnumerationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK)) || EndpointAllocationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK))))) {
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(NETWORK_STATE_DOCUMENT_KIND)) {
NetworkState state = Utils.fromJson(document, NetworkState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(NETWORK_INTERFACE_STATE_DOCUMENT_KIND)) {
NetworkInterfaceState state = Utils.fromJson(document, NetworkInterfaceState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(SECURITY_GROUP_STATE_DOCUMENT_KIND)) {
SecurityGroupState state = Utils.fromJson(document, SecurityGroupState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(SUBNET_STATE_DOCUMENT_KIND)) {
SubnetState state = Utils.fromJson(document, SubnetState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(LOAD_BALANCER_DOCUMENT_KIND)) {
LoadBalancerState state = Utils.fromJson(document, LoadBalancerState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(STORAGE_DESCRIPTION_DOCUMENT_KIND)) {
StorageDescription state = Utils.fromJson(document, StorageDescription.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(RESOURCE_GROUP_DOCUMENT_KIND)) {
ResourceGroupState state = Utils.fromJson(document, ResourceGroupState.class);
if (state.customProperties != null && state.customProperties.containsKey(ResourceGroupService.PROPERTY_NAME_IS_USER_CREATED)) {
continue;
}
if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
// skip resources that have never been attached to a particular endpoint
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(IMAGE_STATE_KIND)) {
ImageState state = Utils.fromJson(document, ImageState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(ROUTER_STATE_KIND)) {
RouterState state = Utils.fromJson(document, RouterState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(AUTH_CREDENTIALS_SERVICE_STATE_KIND)) {
AuthCredentialsServiceState state = Utils.fromJson(document, AuthCredentialsServiceState.class);
if (state.customProperties != null && state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK) != null) {
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK));
} else {
endpointLinkByDocumentLinks.put(state.documentSelfLink, EMPTY_STRING);
}
}
}
}
use of com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState in project photon-model by vmware.
the class AuthCredentialsOperationProcessingChainTest method testEncryptedCredentials.
@Test
public void testEncryptedCredentials() throws Throwable {
// init EncryptionUtils
File keyFile = Paths.get(folder.newFolder().getPath(), "encryption.key").toFile();
System.setProperty(EncryptionUtils.ENCRYPTION_KEY, keyFile.getPath());
System.setProperty(EncryptionUtils.INIT_KEY_IF_MISSING, "true");
EncryptionUtils.initEncryptionService();
AuthCredentialsServiceState credentials = createCredentials("username", "password", false);
assertEquals("username", credentials.userEmail);
assertNotNull(credentials.privateKey);
assertTrue(credentials.privateKey.startsWith(EncryptionUtils.ENCRYPTION_PREFIX));
String publicKey = "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----";
credentials = createCredentialsWithKeys(publicKey, "-----BEGIN PRIVATE KEY-----\nDEF\n-----END PRIVATE KEY-----");
assertEquals(publicKey, credentials.publicKey);
assertNotNull(credentials.privateKey);
assertTrue(credentials.privateKey.startsWith(EncryptionUtils.ENCRYPTION_PREFIX));
// if the private key is (sent) already encrypted, it's not re-encrypted
String encryptedOnce = credentials.privateKey;
String publicKeyNew = "-----BEGIN CERTIFICATE-----\nGHI\n-----END CERTIFICATE-----";
credentials.publicKey = publicKeyNew;
putServiceSynchronously(credentials.documentSelfLink, credentials);
credentials = getServiceSynchronously(credentials.documentSelfLink, AuthCredentialsServiceState.class);
assertEquals(publicKeyNew, credentials.publicKey);
assertNotNull(credentials.privateKey);
assertTrue(credentials.privateKey.startsWith(EncryptionUtils.ENCRYPTION_PREFIX));
assertEquals(encryptedOnce, credentials.privateKey);
// if the private key has changed, it's re-encrypted
credentials.privateKey = "-----BEGIN PRIVATE KEY-----\nJKL\n-----END PRIVATE KEY-----";
putServiceSynchronously(credentials.documentSelfLink, credentials);
credentials = getServiceSynchronously(credentials.documentSelfLink, AuthCredentialsServiceState.class);
assertEquals(publicKeyNew, credentials.publicKey);
assertNotNull(credentials.privateKey);
assertTrue(credentials.privateKey.startsWith(EncryptionUtils.ENCRYPTION_PREFIX));
assertNotEquals(encryptedOnce, credentials.privateKey);
}
Aggregations