Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>com.google.appengine</groupId>
<artifactId>parent</artifactId>
<version>5.0.5-SNAPSHOT</version>
<version>5.0.5-beta.1-SNAPSHOT</version>
</parent>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
Expand Down Expand Up @@ -147,12 +147,43 @@
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-auth</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
</dependency>
</dependencies>
<build>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -213,8 +244,13 @@
<failOnWarnings>false</failOnWarnings>
</configuration>
</plugin>




</plugins>
</build>

<profiles>
<profile>
<id>docFX</id>
Expand Down Expand Up @@ -266,12 +302,12 @@
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.1.1</version>
<version>${auto-service.version}</version>
</path>
<path>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.11.1</version>
<version>${auto-value.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2021 Google LLC
*
* Licensed 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
*
* https://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 com.google.appengine.api;

/** A simple wrapper around {@link System} to allow for easier testing. */
public class SystemEnvironmentProvider implements EnvironmentProvider {
/**
* Gets the value of the specified environment variable.
*
* @param name the name of the environment variable
* @return the string value of the variable, or {@code null} if the variable is not defined
*/
@Override
public String getenv(String name) {
return System.getenv(name);
}

/**
* Gets the value of the specified environment variable, returning a default value if the variable
* is not defined.
*
* @param name the name of the environment variable
* @param defaultValue the default value to return
* @return the string value of the variable, or the default value if the variable is not defined
*/
@Override
public String getenv(String name, String defaultValue) {
String value = System.getenv(name);
return value != null ? value : defaultValue;
}
}
11 changes: 8 additions & 3 deletions api/src/main/java/com/google/appengine/api/images/Composite.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.google.appengine.api.images;

import com.google.appengine.api.images.ImagesServicePb.ImageData;
import java.util.Map;
import java.util.function.Function;

/**
* A {@code Composite} represents a composition of an image onto a canvas.
Expand All @@ -34,10 +36,13 @@ public static enum Anchor {TOP_LEFT, TOP_CENTER, TOP_RIGHT, CENTER_LEFT,

/**
* Adds this compositing operation to a Composite request.
*
* @param request Request for this composite to be added to.
* @param imageIndexMap Map of images and their indexes in the request.
* @param imageDataConverter Function to convert an Image to ImageData.
*/
abstract void apply(ImagesServicePb.ImagesCompositeRequest.Builder request,
Map<Image, Integer> imageIndexMap);

abstract void apply(
ImagesServicePb.ImagesCompositeRequest.Builder request,
Map<Image, Integer> imageIndexMap,
Function<Image, ImageData> imageDataConverter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import static java.util.Objects.requireNonNull;

import com.google.appengine.api.images.ImagesServicePb.CompositeImageOptions;
import com.google.appengine.api.images.ImagesServicePb.ImageData;
import com.google.appengine.api.images.ImagesServicePb.ImagesCompositeRequest;
import java.util.Map;
import java.util.function.Function;

/**
* Implementation of Composite using alpha blending.
Expand Down Expand Up @@ -68,11 +70,14 @@ final class CompositeImpl extends Composite {

/** {@inheritDoc} */
@Override
void apply(ImagesCompositeRequest.Builder request, Map<Image, Integer> imageIndexMap) {
void apply(
ImagesCompositeRequest.Builder request,
Map<Image, Integer> imageIndexMap,
Function<Image, ImageData> imageDataConverter) {
// TODO: What is the purpose of this map?
if (!imageIndexMap.containsKey(image)) {
imageIndexMap.put(image, request.build().getImageCount());
request.addImage(ImagesServiceImpl.convertImageData(image));
request.addImage(imageDataConverter.apply(image));
}
CompositeImageOptions.Builder options = CompositeImageOptions.newBuilder();
int sourceId = requireNonNull(imageIndexMap.get(image));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2021 Google LLC
//
// Licensed 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
//
// https://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 com.google.appengine.api.images;

import com.google.appengine.api.EnvironmentProvider;
import com.google.appengine.api.SystemEnvironmentProvider;
import com.google.appengine.api.images.proto.ImagesServiceGrpc;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.IdTokenCredentials;
import com.google.auth.oauth2.IdTokenProvider;
import com.google.common.annotations.VisibleForTesting;
import static com.google.common.base.Strings.isNullOrEmpty;
import io.grpc.CallCredentials;
import io.grpc.ManagedChannel;
import io.grpc.auth.MoreCallCredentials;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import java.io.IOException;
import java.net.URI;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.net.URISyntaxException;

/** Client for interacting with the gRPC based Images service. */
class GrpcImagesClient {

private static final int MAX_MESSAGE_SIZE = 32 * 1024 * 1024; // 32MB
private final ManagedChannel channel;
private final EnvironmentProvider environmentProvider;
private final CallCredentials callCredentials;

// private ImagesServiceGrpc.ImagesServiceBlockingStub blockingStub;

public GrpcImagesClient() {
this(new SystemEnvironmentProvider(), getApplicationDefaultCredentials());
}

// Constructor for production
GrpcImagesClient(EnvironmentProvider environmentProvider, GoogleCredentials googleCredentials) {
this(environmentProvider, createOidcCredentials(environmentProvider, googleCredentials));
}

// Constructor for testing
@VisibleForTesting
GrpcImagesClient(EnvironmentProvider environmentProvider, CallCredentials callCredentials) {
this.environmentProvider = environmentProvider;
this.callCredentials = callCredentials;
String target = getTarget();
this.channel =
NettyChannelBuilder.forTarget(target)
.maxInboundMessageSize(MAX_MESSAGE_SIZE)
.keepAliveTime(60, SECONDS)
.useTransportSecurity()
.build();
}

private static GoogleCredentials getApplicationDefaultCredentials() {
try {
return GoogleCredentials.getApplicationDefault();
} catch (IOException e) {
throw new IllegalStateException("Failed to get Application Default Credentials", e);
}
}

private String getTarget() {
String endpoint =
environmentProvider.getenv(ImagesServiceFactoryImpl.IMAGES_SERVICE_ENDPOINT_ENV);
if (isNullOrEmpty(endpoint)) {
throw new IllegalStateException(
ImagesServiceFactoryImpl.IMAGES_SERVICE_ENDPOINT_ENV + " environment variable not set.");
}
try {
URI uri = new URI(endpoint);
String host = uri.getHost();
if (host == null) {
throw new IllegalStateException(
"Invalid URI in "
+ ImagesServiceFactoryImpl.IMAGES_SERVICE_ENDPOINT_ENV
+ ": "
+ endpoint);
}
return host + ":443";
} catch (URISyntaxException e) {
throw new IllegalStateException(
"Invalid URI in "
+ ImagesServiceFactoryImpl.IMAGES_SERVICE_ENDPOINT_ENV
+ ": "
+ endpoint,
e);
}
}

private static CallCredentials createOidcCredentials(
EnvironmentProvider environmentProvider, GoogleCredentials googleCredentials) {
String endpoint =
environmentProvider.getenv(ImagesServiceFactoryImpl.IMAGES_SERVICE_ENDPOINT_ENV);
if (isNullOrEmpty(endpoint)) {
throw new IllegalStateException(
ImagesServiceFactoryImpl.IMAGES_SERVICE_ENDPOINT_ENV + " environment variable not set.");
}

if (!(googleCredentials instanceof IdTokenProvider idTokenProvider)) {
throw new IllegalStateException(
"The Application Default Credentials do not support OIDC ID token generation.");
}

IdTokenCredentials idTokenCredentials =
IdTokenCredentials.newBuilder()
.setTargetAudience(endpoint)
.setIdTokenProvider(idTokenProvider)
.build();
return MoreCallCredentials.from(idTokenCredentials);
}

public ImagesServiceGrpc.ImagesServiceBlockingStub getBlockingStub() {
return ImagesServiceGrpc.newBlockingStub(channel).withCallCredentials(callCredentials);
}

public ImagesServiceGrpc.ImagesServiceFutureStub getFutureStub() {
return ImagesServiceGrpc.newFutureStub(channel).withCallCredentials(callCredentials);
}

public void shutdown() {
if (channel != null && !channel.isShutdown()) {
try {
channel.shutdown().awaitTermination(5, SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
// Handle exception
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

package com.google.appengine.api.images;

import com.google.appengine.api.EnvironmentProvider;
import com.google.appengine.api.SystemEnvironmentProvider;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import java.util.Collection;

/**
Expand All @@ -27,9 +32,30 @@
*/
final class ImagesServiceFactoryImpl implements IImagesServiceFactory {

@VisibleForTesting
static final String USE_CUSTOM_IMAGES_GRPC_SERVICE_ENV =
"APPENGINE_USE_CUSTOM_IMAGES_GRPC_SERVICE";

@VisibleForTesting
static final String IMAGES_SERVICE_ENDPOINT_ENV = "APPENGINE_IMAGES_SERVICE_ENDPOINT";

private EnvironmentProvider environmentProvider = new SystemEnvironmentProvider();

private static final Supplier<GrpcImagesClient> grpcClientSupplier =
Suppliers.memoize(() -> new GrpcImagesClient());

@VisibleForTesting
void setEnvironmentProvider(EnvironmentProvider environmentProvider) {
this.environmentProvider = environmentProvider;
}

@Override
public ImagesService getImagesService() {
return new ImagesServiceImpl();
GrpcImagesClient client = null;
if (Boolean.parseBoolean(environmentProvider.getenv(USE_CUSTOM_IMAGES_GRPC_SERVICE_ENV))) {
client = grpcClientSupplier.get();
}
return new ImagesServiceImpl(environmentProvider, client);
}

@Override
Expand All @@ -44,6 +70,12 @@ public Image makeImageFromBlob(BlobKey blobKey) {

@Override
public Image makeImageFromFilename(String filename) {
if (Boolean.parseBoolean(environmentProvider.getenv(USE_CUSTOM_IMAGES_GRPC_SERVICE_ENV))) {
if (!filename.startsWith("/gs/")) {
throw new IllegalArgumentException("Google storage filenames must be prefixed with /gs/");
}
return new ImageImpl(new BlobKey(filename));
}
BlobKey blobKey = BlobstoreServiceFactory.getBlobstoreService().createGsBlobKey(filename);
return new ImageImpl(blobKey);
}
Expand Down
Loading