Search in sources :

Example 6 with AccessToken

use of io.cdap.cdap.security.auth.AccessToken in project cdap by caskdata.

the class RemoteExecutionTwillRunnerService method generateAndSaveRuntimeToken.

/**
 * Generates a runtime token to talk back from the execution cluster to CDAP instance.
 */
private Location generateAndSaveRuntimeToken(ProgramRunId programRunId, Location keysDir) {
    try {
        long currentTimestamp = System.currentTimeMillis();
        // TODO: Use a better identity & expiration
        UserIdentity identity = new UserIdentity(Constants.Security.Authentication.RUNTIME_IDENTITY, UserIdentity.IdentifierType.INTERNAL, Collections.emptyList(), currentTimestamp, currentTimestamp + DEFAULT_EXPIRATION);
        AccessToken accessToken = tokenManager.signIdentifier(identity);
        byte[] encodedAccessToken = Base64.getEncoder().encode(accessTokenCodec.encode(accessToken));
        Location location = keysDir.append(Constants.Security.Authentication.RUNTIME_TOKEN_FILE);
        try (OutputStream os = location.getOutputStream()) {
            os.write(encodedAccessToken);
        }
        return location;
    } catch (IOException e) {
        throw new RuntimeException("Failed to generate runtime token for " + programRunId, e);
    }
}
Also used : AccessToken(io.cdap.cdap.security.auth.AccessToken) UserIdentity(io.cdap.cdap.security.auth.UserIdentity) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Location(org.apache.twill.filesystem.Location)

Aggregations

AccessToken (io.cdap.cdap.security.auth.AccessToken)6 UserIdentity (io.cdap.cdap.security.auth.UserIdentity)4 JsonObject (com.google.gson.JsonObject)3 IOException (java.io.IOException)3 JsonParser (com.google.gson.JsonParser)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 Test (org.junit.Test)2 AccessException (io.cdap.cdap.api.security.AccessException)1 Credential (io.cdap.cdap.proto.security.Credential)1 Principal (io.cdap.cdap.proto.security.Principal)1 InvalidTokenException (io.cdap.cdap.security.auth.InvalidTokenException)1 OutputStream (java.io.OutputStream)1 Location (org.apache.twill.filesystem.Location)1