|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigtable.data.v2; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.auth.oauth2.OAuth2Utils; |
| 22 | +import com.google.auth.oauth2.ServiceAccountJwtAccessCredentials; |
| 23 | +import com.google.cloud.bigtable.data.v2.internal.JwtCredentialsWithAudience; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.URI; |
| 26 | +import java.security.PrivateKey; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +public class JwtCredentialsWithAudienceTests { |
| 30 | + |
| 31 | + private static final String SA_CLIENT_EMAIL = |
| 32 | + "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr@developer.gserviceaccount.com"; |
| 33 | + private static final String SA_CLIENT_ID = |
| 34 | + "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com"; |
| 35 | + private static final String SA_PRIVATE_KEY_ID = "d84a4fefcf50791d4a90f2d7af17469d6282df9d"; |
| 36 | + private static final String SA_PRIVATE_KEY_PKCS8 = |
| 37 | + "-----BEGIN PRIVATE KEY-----\n" |
| 38 | + + "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALX0PQoe1igW12ikv1bN/r9lN749y2ijmbc/mFHPyS3hNTyOCjDvBbXYbDhQJzWVUikh4mvGBA07qTj79Xc3yBDfKP2IeyYQIFe0t0zkd7R9Zdn98Y2rIQC47aAbDfubtkU1U72t4zL11kHvoa0/RuFZjncvlr42X7be7lYh4p3NAgMBAAECgYASk5wDw4Az2ZkmeuN6Fk/y9H+Lcb2pskJIXjrL533vrDWGOC48LrsThMQPv8cxBky8HFSEklPpkfTF95tpD43iVwJRB/GrCtGTw65IfJ4/tI09h6zGc4yqvIo1cHX/LQ+SxKLGyir/dQM925rGt/VojxY5ryJR7GLbCzxPnJm/oQJBANwOCO6D2hy1LQYJhXh7O+RLtA/tSnT1xyMQsGT+uUCMiKS2bSKx2wxo9k7h3OegNJIu1q6nZ6AbxDK8H3+d0dUCQQDTrPSXagBxzp8PecbaCHjzNRSQE2in81qYnrAFNB4o3DpHyMMY6s5ALLeHKscEWnqP8Ur6X4PvzZecCWU9BKAZAkAutLPknAuxSCsUOvUfS1i87ex77Ot+w6POp34pEX+UWb+u5iFn2cQacDTHLV1LtE80L8jVLSbrbrlH43H0DjU5AkEAgidhycxS86dxpEljnOMCw8CKoUBd5I880IUahEiUltk7OLJYS/Ts1wbn3kPOVX3wyJs8WBDtBkFrDHW2ezth2QJADj3e1YhMVdjJW5jqwlD/VNddGjgzyunmiZg0uOXsHXbytYmsA545S8KRQFaJKFXYYFo2kOjqOiC1T2cAzMDjCQ==\n" |
| 39 | + + "-----END PRIVATE KEY-----\n"; |
| 40 | + private static final String QUOTA_PROJECT = "sample-quota-project-id"; |
| 41 | + |
| 42 | + @Test |
| 43 | + public void getUniverseDomain_default() throws IOException { |
| 44 | + PrivateKey privateKey = OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); |
| 45 | + ServiceAccountJwtAccessCredentials serviceAccountJwtAccessCredentials = |
| 46 | + ServiceAccountJwtAccessCredentials.newBuilder() |
| 47 | + .setClientId(SA_CLIENT_ID) |
| 48 | + .setClientEmail(SA_CLIENT_EMAIL) |
| 49 | + .setPrivateKey(privateKey) |
| 50 | + .setPrivateKeyId(SA_PRIVATE_KEY_ID) |
| 51 | + .setQuotaProjectId(QUOTA_PROJECT) |
| 52 | + .build(); |
| 53 | + JwtCredentialsWithAudience jwtWithAudience = |
| 54 | + new JwtCredentialsWithAudience( |
| 55 | + serviceAccountJwtAccessCredentials, URI.create("default-aud")); |
| 56 | + assertThat(jwtWithAudience.getUniverseDomain()).isEqualTo("googleapis.com"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void getUniverseDomain_custom() throws IOException { |
| 61 | + PrivateKey privateKey = OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8); |
| 62 | + ServiceAccountJwtAccessCredentials serviceAccountJwtAccessCredentials = |
| 63 | + ServiceAccountJwtAccessCredentials.newBuilder() |
| 64 | + .setClientId(SA_CLIENT_ID) |
| 65 | + .setClientEmail(SA_CLIENT_EMAIL) |
| 66 | + .setPrivateKey(privateKey) |
| 67 | + .setPrivateKeyId(SA_PRIVATE_KEY_ID) |
| 68 | + .setQuotaProjectId(QUOTA_PROJECT) |
| 69 | + .setUniverseDomain("example.com") |
| 70 | + .build(); |
| 71 | + JwtCredentialsWithAudience jwtWithAudience = |
| 72 | + new JwtCredentialsWithAudience( |
| 73 | + serviceAccountJwtAccessCredentials, URI.create("default-aud")); |
| 74 | + assertThat(jwtWithAudience.getUniverseDomain()).isEqualTo("example.com"); |
| 75 | + } |
| 76 | +} |
0 commit comments