Skip to content

Commit

Permalink
Force azure impl to be public
Browse files Browse the repository at this point in the history
The azure sdk internally dynamically constructs its client classes.
However, one of these, for the blob storage is private. This has been
fixed upstream, but until that is released, the client is unuseable
without the newProxyInPackage permission. Rather than grant that
permission, this commit makes the class in question public by patching
the azure class file when building the azure repository plugin.

relates Azure/azure-sdk-for-java#17368
  • Loading branch information
rjernst committed Dec 2, 2020
1 parent c47fcf4 commit 0f522ad
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 10 deletions.
2 changes: 2 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ dependencies {
api 'com.avast.gradle:gradle-docker-compose-plugin:0.13.4'
api 'org.apache.maven:maven-model:3.6.2'
api 'com.networknt:json-schema-validator:1.0.36'
api 'org.ow2.asm:asm:9.0'
api 'org.ow2.asm:asm-tree:9.0'
api "org.apache.httpcomponents:httpclient:${props.getProperty('httpclient')}"
api "org.apache.httpcomponents:httpcore:${props.getProperty('httpcore')}"
compileOnly "com.puppycrawl.tools:checkstyle:${props.getProperty('checkstyle')}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.gradle;

import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import static org.objectweb.asm.Opcodes.ACC_PUBLIC;

/**
* A task to manipulate an existing class file.
*/
public class JavaClassPublicifier extends DefaultTask {

private String classFile;
private DirectoryProperty inputDir;
private DirectoryProperty outputDir;

public JavaClassPublicifier() {
this.inputDir = getProject().getObjects().directoryProperty();
this.outputDir = getProject().getObjects().directoryProperty();
}

@Input
public String getClassFile() {
return classFile;
}

public void setClassFile(String classFile) {
this.classFile = classFile;
}

@InputDirectory
public DirectoryProperty getInputDir() {
return inputDir;
}

@OutputDirectory
public DirectoryProperty getOutputDir() {
return outputDir;
}

@TaskAction
public void adapt() throws IOException {
final ClassNode classNode;
try (InputStream is = Files.newInputStream(inputDir.get().file(classFile).getAsFile().toPath())) {
ClassReader classReader = new ClassReader(is);
classNode = new ClassNode();
classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
}

classNode.access |= ACC_PUBLIC;

ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
classNode.accept(classWriter);

File outputFile = outputDir.get().file(classFile).getAsFile();
outputFile.getParentFile().mkdirs();
Files.write(outputFile.toPath(), classWriter.toByteArray());
}
}
32 changes: 32 additions & 0 deletions plugins/repository-azure/azure-storage-blob/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.elasticsearch.gradle.JavaClassPublicifier;

apply plugin: 'elasticsearch.java'
apply plugin: 'com.github.johnrengelman.shadow'

configurations {
originalJar {
transitive = false
}
}

dependencies {
originalJar "com.azure:azure-storage-blob:${project.parent.versions.azure}"
implementation "com.azure:azure-storage-blob:${project.parent.versions.azure}"
}

String blobsServiceClass = 'com/azure/storage/blob/implementation/BlobsImpl$BlobsService.class'

tasks.create('extractBlobsServiceClass', Copy).configure {
from({ zipTree(configurations.originalJar.singleFile) }) {
include blobsServiceClass
}
into project.file('build/original')
}

tasks.create('makeBlobsServicePublic', JavaClassPublicifier).configure {
classFile = blobsServiceClass
inputDir = project.layout.buildDirectory.dir('original')
outputDir = project.layout.buildDirectory.dir('modified')
}

sourceSets.main.java.compiledBy(tasks.named('makeBlobsServicePublic')) { myTask -> myTask.outputDir }
9 changes: 1 addition & 8 deletions plugins/repository-azure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ versions << [
]

dependencies {
api "com.azure:azure-storage-blob:${versions.azure}"
api project(path: 'azure-storage-blob', configuration: 'shadow')
api "com.azure:azure-storage-blob-batch:12.6.0"
api "com.azure:azure-storage-common:${versions.azure}"
api "com.azure:azure-core-http-netty:${versions.azureCoreHttpNetty}"
Expand Down Expand Up @@ -141,12 +141,6 @@ tasks.named("thirdPartyAudit").configure {
'kotlin.reflect.KDeclarationContainer',
'kotlin.sequences.Sequence',

// from com.azure.storage.blob.changefeed
'com.azure.storage.internal.avro.implementation.AvroObject',
'com.azure.storage.internal.avro.implementation.AvroReader',
'com.azure.storage.internal.avro.implementation.AvroReaderFactory',
'com.azure.storage.internal.avro.implementation.schema.AvroSchema',

// from io.netty.handler.codec.protobuf.ProtobufDecoder (netty)
'com.google.protobuf.ExtensionRegistry',
'com.google.protobuf.MessageLite$Builder',
Expand Down Expand Up @@ -305,7 +299,6 @@ tasks.named("thirdPartyAudit").configure {
'reactor.core.publisher.UnsafeSupport'
)
}

boolean useFixture = false

def azureAddress = {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
grant {
// azure client opens socket connections for to access repository
permission java.net.SocketPermission "*", "connect";
permission java.lang.reflect.ReflectPermission "newProxyInPackage.com.azure.storage.blob.implementation";
// io.netty.util.concurrent.GlobalEventExecutor.startThread
permission java.lang.RuntimePermission "setContextClassLoader";
// Used by jackson bean deserialization
Expand Down

0 comments on commit 0f522ad

Please sign in to comment.