Skip to content

Shade relocation changes the address of metadata server then compute engine credentials get stuck #503

@yunus

Description

@yunus

I have encountered a corner case which emerges when I generate my shaded uber jar. I have realized that shaded jar is not able to get service account credentials in a VM/GKE pod. When I decompiled the shaded jars, I have realized that the gcp metadata server path is prefixed with /com/google/cloud/spanner/jdbc/shaded .
As a solution I have changed pattern com to com. and I've applied similarly to io., org. and android.

Environment details

  1. spanner-jdbc-driver
  2. macos
  3. Java version: 8
  4. spanner-jdbc version(s): master

Steps to reproduce

  1. mvn package -Pshade -DskipTests
  2. cd target
  3. unzip google-cloud-spanner-jdbc-*-single-jar-with-dependencies.jar
  4. cd com/google/cloud/spanner/jdbc/shaded/com/google/auth/oauth2
  5. decompile ComputeEngineCredentials.class , you will see
public static String getServiceAccountsUrl() {
   return getMetadataServerUrl(DefaultCredentialsProvider.DEFAULT) + "/com/google/cloud/spanner/jdbc/shaded/computeMetadata/v1/instance/service-accounts/?recursive=true";
 }

As you see above the metadata server path is prefixed by shaded address.

Solution

I have excluded the computeMetadata in pom.xml by changing the patterns.

<relocations>
                <relocation>
                  <pattern>com.</pattern>
                  <shadedPattern>com.google.cloud.spanner.jdbc.shaded.com.</shadedPattern>
                  <excludes>
                    <exclude>com.google.cloud.spanner.**</exclude>
                  </excludes>
                </relocation>
                <relocation>
                  <pattern>android.</pattern>
                  <shadedPattern>com.google.cloud.spanner.jdbc.shaded.android.</shadedPattern>
                </relocation>
                <relocation>
                  <pattern>io.</pattern>
                  <shadedPattern>com.google.cloud.spanner.jdbc.shaded.io.</shadedPattern>
                  <excludes>
                    <exclude>io.grpc.netty.**</exclude>
                  </excludes>
                </relocation>
                <relocation>
                  <pattern>org.</pattern>
                  <shadedPattern>com.google.cloud.spanner.jdbc.shaded.org.</shadedPattern>
                  <excludes>
                    <exclude>org.conscrypt.**</exclude>
                  </excludes>
                </relocation>
              </relocations>

Any additional information below

I have encountered this while trying to integrate cloud spanner to an old codebase.
Also I have realized that many libraries are dragged by shaded pom so when I add shaded jar as a dependency I have excluded everything:

<dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-spanner-jdbc</artifactId>
      <version>2.2.1-shaded</version>
      <classifier>single-jar-with-dependencies</classifier>
        <exclusions>
            <exclusion>
                <groupId>*</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Metadata

Metadata

Assignees

Labels

api: spannerIssues related to the googleapis/java-spanner-jdbc API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions