GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Make authentication simpler · Issue #3 · googleapis/google-cloud-java

Make authentication simpler #3

New issue
Copy link
New issue
Copy link
Closed
Closed
Make authentication simpler#3
Copy link
Assignees
jgeewax
Labels
api: coreauthtype: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@jgeewax
jgeewax
opened on Feb 10, 2015
Issue body actions

What we do today:

final KeyStore keystore = KeyStore.getInstance("PKCS12");
final FileInputStream fis = new FileInputStream("/path/to/key.p12");
keystore.load(fis, "password".toCharArray());
final PrivateKey key = (PrivateKey) keystore.getKey("privatekey", "password".toCharArray());

final AuthConfig ac = AuthConfig.createFor("[email protected]", key);

final DatastoreServiceOptions options = DatastoreServiceOptions.builder()
    .dataset(datasetId)
    .authConfig(ac)
    .build();

final DatastoreService datastore = DatastoreServiceFactory.getDefault(options);

What we'd like to do:

final Credential credential = DatastoreHelper.getServiceAccountCredential(
    "[email protected]", "/path/to/key.p12");
final DatastoreOptions options = DatastoreHelper.getOptionsfromEnv()
  .dataset(datasetId)
  .credential(credential)
  .build();
final Datastore datastore = DatastoreFactory.get().create(options);

Activity

Next
  1. jgeewax
    modified the milestone: This milestone has been deleted on Feb 10, 2015
  2. jgeewax
    added
    auth
    api: core
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Feb 10, 2015
  3. aozarov commented on Feb 10, 2015

    @aozarov
    aozarov
    on Feb 10, 2015
    Contributor
    More actions

    It is much simpler when running in GCP (AE or CE) when we "auto-detect" the dataset and use the instance credentials.

    Though we should provide "helpers" for typical boilerplate code, I am a little hesitant to add code that will work in some environment but not in other (e.g. The use of files (and providing a full path) is not going to work on App Engine).

    An alternative that may work for all would be to load the key bytes as a resource.

    Also, the suggestion above is only going to work when the password for that keystore is fixed (e.g. "notasecret") so I think the helper should provide a way to supply a different password.

    As for getDatastoreFromEnv() it is already partially imlemented, though in an implicit way. When DatastoreServiceOptions.Builder#build is called if a dataset was not provided the
    DATASTORE_DATASET System property or Environment variable will be considered. We should probably do that also for DATASTORE_NAMESPACE, SERVICE_ACCOUNT, PRIVATE_KEY_FILE, PRIVATE_KEY_PASSWORD (looks like this could be generic for all gcloud services and not tied to datastore).

    Also, env/property settings need to be documented (probably in the matching builder's setter or getter..).
    BTW, if providing a way to configure auth via system/env properties do you still find much value in the programmatic helper (which is basically a "PrivateKey" helper...)

  4. aozarov commented on Mar 18, 2015

    @aozarov
    aozarov
    on Mar 18, 2015
    Contributor
    More actions

    Commit a6cae12 is using now the new authkit library and when default are used (no explicit auth config settings) we will follow the gcloud authentication standard which includes using a file referenced by GOOGLE_APPLICATION_CREDENTIALS environment variable. @jgeewax do you still think we need a helper for reading PK12 files in this case?

  5. aozarov
    assigned
    jgeewax
    on Mar 18, 2015
  6. aozarov commented on Jun 3, 2015

    @aozarov
    aozarov
    on Jun 3, 2015
    Contributor
    More actions

    ping.

  7. jgeewax commented on Jun 3, 2015

    @jgeewax
    jgeewax
    on Jun 3, 2015
    Author
    More actions

    Now that we're using a JSON keyfile (right?), I think the simplest and happiest way to get a Datastore instance would be...

    final Datastore datastore = DatastoreHelper.getOptionsfromEnv()
      .dataset(datasetId)
      .keyFile("/path/to/keyfile.json") // Optional if you are in GCE or GAE
      .buildDatastore();

    Or something similar. Is that crazy?

  8. aozarov commented on Jun 5, 2015

    @aozarov
    aozarov
    on Jun 5, 2015
    Contributor
    More actions

    Not necessarily. Do we mandate/accept only json file in the other Veneer libraries (rather than also accepting a p12 key file as well)?

    I was trying to see if I can extract the service email from the key (some information is there) but wasn't able to do it using the Java APis.

    Using a path for a file is nice but java File is not accessible on all platform and also does not cover all options (e.g. it could be provided as a bundled resource). This is why I leave it for the user to construct the Key (as described above - we could actually have that as snippet as an example in the specific Auth config class javadoc).

    Another option instead of File would be to accept a stream which is likely to support all various input type.
    In that case these questions are still open:

    • Do we only support json file (for getting the service email) or should we support both file types.
    • Do we only work with keys that uses the default, notasecret, password?

    I think we should keep discussion the other suggestion of creating the service directly from the options in #54.

  9. jgeewax commented on Jun 6, 2015

    @jgeewax
    jgeewax
    on Jun 6, 2015
    Author
    More actions

    Not necessarily. Do we mandate/accept only json file in the other Veneer libraries (rather than also accepting a p12 key file as well)?

    Other libraries accept all of the options. Is it crazy to look up the file and check the extension, and then operate differently based on that? If so, then we could offer ... .p12Key('...') and jsonKey('...') right?

    I was trying to see if I can extract the service email from the key (some information is there) but wasn't able to do it using the Java APis.

    I was under the impression that you can't get the service account e-mail from a p12 key, which was the point of the JSON keyfile (it includes all the information needed).

    Using a path for a file is nice but java File is not accessible on all platform and also does not cover all options (e.g. it could be provided as a bundled resource). This is why I leave it for the user to construct the Key (as described above - we could actually have that as snippet as an example in the specific Auth config class javadoc).

    That makes sense. So can we simplify the "get a key" process? Maybe a fromFile() method? My concern is that the following piece is super long.

    final KeyStore keystore = KeyStore.getInstance("PKCS12");
    final FileInputStream fis = new FileInputStream("/path/to/key.p12");
    keystore.load(fis, "password".toCharArray());
    final PrivateKey key = (PrivateKey) keystore.getKey("privatekey", "password".toCharArray());
    final AuthConfig ac = AuthConfig.createFor("[email protected]", key);

    Another option instead of File would be to accept a stream which is likely to support all various input type.

    I really like that idea! See my note above about the different forms of auth. The most common will be JSON, the next would be p12 -- nothing stopping us from having multiple "set the key for this builder" methods where the user tells us what kind of key is in the stream.

    Do we only work with keys that uses the default, notasecret, password?

    I believe so. Currently (AFAIK) there's no way to create a key with a custom password.

  10. aozarov commented on Sep 25, 2015

    @aozarov
    aozarov
    on Sep 25, 2015
    Contributor
    More actions

    @jgeewax do you think we can close this issue now?

    Change #180 does not make it easier to load a pkcs12 file but does make it simpler to work with a json file. See #179 for more details about why we chose only to simplify the json way.

  11. jgeewax commented on Sep 29, 2015

    @jgeewax
    jgeewax
    on Sep 29, 2015
    Author
    More actions

    @mziccard Can you summarize for me what the auth experience is like in gcloud-java world?

    For example, if I want to be explicit about my keyfile and project ID, and get an entity from Datastore in gcloud-node, I do:

    var config = {
      projectId: 'grape-spaceship-123',
      keyFilename: '/path/to/keyfile.json'
    };
    
    var gcloud = require('gcloud')(config);
    var dataset = gcloud.datastore.dataset();
    var entity = dataset.get(dataset.key(['Product', 123]));

    (see http://googlecloudplatform.github.io/gcloud-node/#/authentication)

    If I wanted to do similarly with Java (let's say I just wanted to get an entity from Datastore), what would that code look like (the explicit version)?

  12. mziccard commented on Sep 29, 2015

    @mziccard
    mziccard
    on Sep 29, 2015
    Contributor
    More actions

    @jgeewax Now you can do something like:

    DatastoreOptions options = DatastoreOptions.builder()
        .projectId("grape-spaceship-123")
        .authCredentials(AuthCredentials.createForJson(
            new FileInputStream("/path/to/keyfile.json")))
        .build();
    Datastore datastore = DatastoreFactory.instance().get(options);
    KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
    Key key = keyFactory.newKey(keyName);
    Entity entity = datastore.get(key);
  13. 3 remaining items

  14. qian-long
    added a commit that references this issue on Feb 12, 2019

    Import of package:google-cloud-spanner to GitHub Public Repo.

    3acaac5
  15. github-actions
    added a commit that references this issue on Jul 1, 2022

    chore: release 0.1.0 (#3)

    1befaf6
  16. github-actions
    added 4 commits that reference this issue on Aug 16, 2022

    Merge pull request #3 from renovate-bot/renovate/org.apache.maven.plu…

    4dc55a5

    build(deps): update dependency org.apache.maven.plugins:maven-project…

    f924697

    build(deps): update dependency org.apache.maven.plugins:maven-deploy-…

    74d137a

    build(deps): update dependency org.apache.maven.plugins:maven-deploy-…

    7f9ee3b
  17. github-actions
    added a commit that references this issue on Aug 25, 2022

    build(deps): update dependency org.apache.maven.plugins:maven-deploy-…

    8a04276
  18. github-actions
    added a commit that references this issue on Sep 15, 2022

    build(deps): update dependency org.apache.maven.plugins:maven-deploy-…

    53bcae4
  19. chingor13
    added a commit that references this issue on Jan 6, 2026

    chore: make java-logging-logback releasable (#3)

    00ee26b
  20. chingor13
    added a commit that references this issue on Jan 22, 2026

    fix: remove temporary added animal-sniffer-annotations exclusion rule…

    This issue will close once commit fde0a9f is merged into the 'main' branch.
    fde0a9f
  21. chingor13
    added a commit that references this issue on Feb 24, 2026

    Add asynchronous getRequestMetadata.

    This issue will close once commit fe87b7f is merged into the 'main' branch.
    fe87b7f
  22. chingor13
    added a commit that references this issue on Mar 12, 2026

    Add asynchronous getRequestMetadata.

    This issue will close once commit 9e1e9d4 is merged into the 'main' branch.
    9e1e9d4
  23. meltsufin
    added 2 commits that reference this issue on Apr 29, 2026

    feat: make repo releasable, add parent/bom (#3)

    53c4030

    feat: make repo releasable, add parent/bom (#3)

    9186d83
  24. jinseopkim0
    added a commit that references this issue on May 12, 2026

    chore: partial release of datastore as 1.86.0 #3 (#13170)

    dc965fe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @jgeewax
    jgeewax

Labels

api: coreauthtype: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.