static final SecureRandom secureRandom = new SecureRandom();
@POST
@Produces({ JSON })
public WrappedCredential createEc2Credential() throws CloudException, DuplicateValueException {
UserData forUser = getUser(Long.valueOf(userId));
WrappedCredential response = new WrappedCredential();
TokenInfo tokenInfo = findTokenInfo();
if (tokenInfo == null) {
throw new WebApplicationException(Status.UNAUTHORIZED);
}
long projectId = tokenInfo.getProjectId();
if (projectId == 0) {
throw new WebApplicationException(Status.UNAUTHORIZED);
}
String accessId;
ByteString secret;
{
byte[] r = new byte[16];
synchronized (secureRandom) {
secureRandom.nextBytes(r);
}
secret = ByteString.copyFrom(r);
}
{
AccessId.Builder b = AccessId.newBuilder();
b.setUserId(forUser.getId());
byte[] r = new byte[8];
synchronized (secureRandom) {
// We don't technically need secure random here, but we want it
// later!
secureRandom.nextBytes(r);
}
b.setPadding(ByteString.copyFrom(r));
accessId = Hex.toHex(b.build().toByteArray());
}
CredentialData created;
{
CredentialData.Builder b = CredentialData.newBuilder();
b.setUserId(forUser.getId());
b.setProjectId(projectId);
b.setKey(accessId);
b.setDeprecatedSharedSecret(secret);
created = authRepository.getEc2Credentials().create(b);