Skip to content

Conversation

@rmehta19
Copy link
Contributor

@rmehta19 rmehta19 commented Oct 29, 2024

Modify the Client Libraries gRPC Channel builder to use mTLS via S2A if the experimental environment variable is set, S2A is available (We check this by using utility added in googleapis/google-auth-library-java#1400), and a few more conditions (see shouldUseS2A).

Following https://google.aip.dev/auth/4115, Only attempt to use S2A after DirectPath and DCA (https://google.aip.dev/auth/4114) are ruled out as options. If conditions to use S2A are not met (env variable not set, or S2A is not running in environment, etc (shouldUseS2A returns false)), fall back to default TLS connection.

When we are creating S2A-enabled Grpc Channel Credentials, we first try to secure the connection between the client and the S2A via MTLS, using MTLS-MDS credentials. If MTLS-MDS credentials can't be loaded, then we fallback to a plaintext connection between the client and S2A.

The parallel go implementation : googleapis/google-api-go-client#1874 (now lives here: https://github.com/googleapis/google-cloud-go/blob/main/auth/internal/transport/cba.go)

S2A Java client: https://github.com/grpc/grpc-java/tree/master/s2a

Resolving b/376258193 means that S2A.java is no longer experimental

@conventional-commit-lint-gcf
Copy link

conventional-commit-lint-gcf bot commented Oct 29, 2024

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@product-auto-label product-auto-label bot added the size: xs Pull request size is extra small. label Oct 29, 2024
@rmehta19 rmehta19 changed the title Implement AIP#4114 Implement AIP#4115: create mTLS connection using S2A Oct 29, 2024
@rmehta19 rmehta19 marked this pull request as draft October 29, 2024 23:29
@sonarqubecloud
Copy link

@sonarqubecloud
Copy link

Quality Gate Passed Quality Gate passed for 'java_showcase_integration_tests'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: xs Pull request size is extra small. labels Oct 31, 2024
@rmehta19 rmehta19 force-pushed the grpc-channel-using-s2a branch from d6ba979 to 68d0c14 Compare November 1, 2024 14:39
@rmehta19 rmehta19 force-pushed the grpc-channel-using-s2a branch from 68d0c14 to 3510643 Compare November 1, 2024 14:59
@rmehta19 rmehta19 marked this pull request as ready for review November 4, 2024 13:27
@lqiu96 lqiu96 requested review from blakeli0, lqiu96 and zhumin8 November 4, 2024 15:42
@lqiu96
Copy link
Member

lqiu96 commented Nov 4, 2024

Thanks @rmehta19, can we do a few things to help clean up the PR for reviews:

  1. Can you add a conventional commit style title to the PR? I think something like feat: {...} would help us and users understand the changes that went in the PR/ release
  2. Can we split the deps upgrades into separate PRs? We typically use renovate-bot to merge deps and I believe we just merged. the ones you are looking for.

// Try to load MTLS-MDS creds.
File rootFile = new File(MTLS_MDS_ROOT);
File certKeyFile = new File(MTLS_MDS_CERT_CHAIN_AND_KEY);
if (!rootFile.isFile() || !certKeyFile.isFile()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be if (rootFile.isFile() && certKeyFile.isFile())? Or refactor this to

if (!rootFile.isFile() || !certKeyFile.isFile()) {
  return createPlaintextToS2AChannelCredentials(plaintextAddress);
}

to reduce indentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry this is my bad! Changed to if (rootFile.isFile() && certKeyFile.isFile()). Thanks for catching! 1ff7a92

Copy link
Contributor

@blakeli0 blakeli0 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think that means we don't have any test coverage for createS2ASecuredChannelCredentials() though, and to some extend, also no test coverage for the logics in createSingleChannel().

It's probably hard to test the channels created from createSingleChannel() since there are no getters in ManagedChannel for us to verify it, and that's probably we don't have much test for it in the past either. For the new method createS2ASecuredChannelCredentials(), we could probably extract the file path as parameters of this method so that we can unit test it properly.


// The public portion of the mTLS MDS root certificate is stored for performing
// cert verification when establishing an mTLS connection with the MDS.
private static final String MTLS_MDS_ROOT = "/run/google-mds-mtls/root.crt";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these are well-known locations on GCE? Do we have any public or internal docs for these locations? If not, where do we get these? What I'm worried is that how do we get notified if they change?

Copy link
Contributor Author

@rmehta19 rmehta19 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these are well-known locations on GCE?

Yes

Do we have any public or internal docs for these locations?

I included a link down below in the logic, I've moved it up here as well: https://cloud.google.com/compute/docs/metadata/overview#https-mds. Specifically https://cloud.google.com/compute/docs/metadata/overview#https-mds-root-certs and https://cloud.google.com/compute/docs/metadata/overview#https-mds-client-certs

2958fb4

Copy link
Contributor

@blakeli0 blakeli0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. There is a CI checking failing though, we might need to update some dependencies for self-service, @lqiu96 can you please take a look?

@lqiu96
Copy link
Member

lqiu96 commented Nov 14, 2024

/gcbrun

Comment on lines +528 to +534
} else {
// Fallback to plaintext-to-S2A connection if MTLS-MDS creds do not exist.
LOG.log(
Level.INFO,
"Cannot establish an mTLS connection to S2A because MTLS to MDS credentials do not exist on filesystem, falling back to plaintext connection to S2A");
return createPlaintextToS2AChannelCredentials(plaintextAddress);
}
Copy link
Member

@lqiu96 lqiu96 Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Perhaps this else block could be an early return instead
i.e.

if (!rootFile.isFile() || !certKeyFile.isFile()) {
...
}
{Logic to create s2a}

would reduce the nesting. Ignore this comment if this was already discussed.

PR LGTM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I also suggested it in #3326 (comment), but it does not have to block this PR.

@blakeli0 blakeli0 merged commit 1138ca6 into googleapis:main Nov 14, 2024
46 of 51 checks passed
@ejona86
Copy link

ejona86 commented Nov 22, 2024

This mustn't depend on io.grpc.s2a.S2AChannelCredentials because it is @ExperimentalApi. Libraries can't choose the versions of the libraries they use. We'd need to stabilize the API for use here.

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

Labels

size: l Pull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants