Search code examples
google-cloud-platformjcloudsgoogle-compute-engine

Manual authentication for Google API in jclouds, separating token acquisition


I need to separate the authentication phase from Google's Api creation, but it's very difficult (for me) to make it possible.

This is very important because I am creating a REST API that should receive the authorization tokens previously acquired and not the credentials directly from its users for security reasons, because with tokens I can set a lifetime limit as specified in RFC 6750.

I have the following code:

public class Main { 

    public static void main(String[] args) {      
      
        // Reads the JSON credential file provided by Google
        String jsonContent = readJson(args[1]);  
        
        // Pass the credential content
        GoogleComputeEngineApi googleApi = 
                createApi(jsonContent); 
    }
    
    public static GoogleComputeEngineApi createApi(final String jsonCredentialContent) {
        try {
            Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(
                    jsonCredentialContent);

            ComputeServiceContext context = ContextBuilder
                    .newBuilder("google-compute-engine")
                    .credentialsSupplier(credentialSupplier)
                    .buildView(ComputeServiceContext.class);

            Credentials credentials = credentialSupplier.get();
            ContextBuilder contextBuilder = ContextBuilder
                    .newBuilder(GoogleComputeEngineProviderMetadata.builder()
                            .build())
                    .credentials(credentials.identity, credentials.credential);

            Injector injector = contextBuilder.buildInjector();
            return injector.getInstance(GoogleComputeEngineApi.class);
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }  
}

Below is a fake code with my needs:

public class Main { 
            
    public static void main(String[] args) {        
        
        String jsonCredentialContent = readJson(args[1]);  
        String oauthToken = "";
        
        // First acquires the OAuth token
        if(getAuthenticationType("google-compute-engine").equals("oauth")) {
            oauthToken = getTokenForOAuth(jsonCredentialContent);
        }        
                    
        // Creates the Api with the previously acquired token
        GoogleComputeEngineApi googleApi = 
                createApi(oauthToken); 
    }       
    
    [...]
    
}

Solution

  • You can directly use the jclouds OAuth API to get the bearer token, as follows:

    GoogleCredentialsFromJson credentials = new GoogleCredentialsFromJson(jsoncreds);
    
    AuthorizationApi oauth = ContextBuilder.newBuilder("google-compute-engine")
        .credentialsSupplier(credentials)
        .buildApi(AuthorizationApi.class);
    
    try {
        long nowInSeconds = System.currentTimeMillis() / 1000;
        Claims claims = Claims.create(
            credentials.get().identity, // issuer
            "https://www.googleapis.com/auth/compute", // write scope
            "https://accounts.google.com/o/oauth2/token", // audience
            nowInSeconds + 60, // token expiration (seconds)
            nowInSeconds // current time (secods)
        );
        Token token = oauth.authorize(claims);
        System.out.println(token);
    } finally {
        oauth.close();
    }
    

    Once you have the Bearer access token you can create the jclouds context with it as follows:

    // Override GCE default Oauth flow (JWT) by the Bearer token flow
    Properties overrides = new Properties();
    overrides.put(OAuthProperties.CREDENTIAL_TYPE, CredentialType.BEARER_TOKEN_CREDENTIALS.toString());
    
    // It is important to set the proper identity too, as it is used to resolve the GCE project
    ComputeServiceContext ctx = ContextBuilder.newBuilder("google-compute-engine")
        .overrides(overrides)
        .credentials(credentials.get().identity, token.accessToken())
        .buildView(ComputeServiceContext.class);
    
    GoogleComputeEngineApi google = ctx.unwrapApi(GoogleComputeEngineApi.class);