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
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2017-2020 the original author or authors.
*
* 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.cloud.spring.autoconfigure.emulator;

import com.google.auth.Credentials;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Custom credentials provider used when connecting to a local Firestore emulator.
*/
public class FirestoreEmulatorCredentials extends Credentials {

private final Map<String, List<String>> headerMap;

/**
* Constructs emulator credentials targeting the specified root resource path.
*
* @param rootPath the Firestore root database path
*/
public FirestoreEmulatorCredentials(String rootPath) {
this.headerMap = new HashMap<>();
this.headerMap.put("Authorization", Collections.singletonList("Bearer owner"));
this.headerMap.put(
"google-cloud-resource-prefix",
Collections.singletonList(rootPath.substring(0, rootPath.lastIndexOf("/documents"))));
}

@Override
public String getAuthenticationType() {
return null;
}

@Override
public Map<String, List<String>> getRequestMetadata(URI uri) {
return this.headerMap;
}

@Override
public boolean hasRequestMetadata() {
return true;
}

@Override
public boolean hasRequestMetadataOnly() {
return true;
}

@Override
public void refresh() {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.google.cloud.spring.data.firestore.repository.config.FirestoreRepositoryConfigurationExtension;
import java.lang.annotation.Annotation;
import org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;

/**
Expand All @@ -47,10 +45,6 @@ protected RepositoryConfigurationExtension getRepositoryConfigurationExtension()
return new FirestoreRepositoryConfigurationExtension();
}

@EnableReactiveFirestoreRepositories(
excludeFilters =
@ComponentScan.Filter(
type = FilterType.REGEX,
pattern = ".*GcpFirestoreEmulatorAutoConfiguration.*"))
@EnableReactiveFirestoreRepositories
private static class EnableFirestoreRepositoriesConfiguration {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.auth.Credentials;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.spring.autoconfigure.emulator.FirestoreEmulatorCredentials;
import com.google.cloud.spring.autoconfigure.firestore.GcpFirestoreAutoConfiguration.FirestoreReactiveAutoConfiguration;
import com.google.cloud.spring.data.firestore.FirestoreTemplate;
import com.google.cloud.spring.data.firestore.mapping.FirestoreClassMapper;
Expand All @@ -27,11 +28,6 @@
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.auth.MoreCallCredentials;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand Down Expand Up @@ -77,37 +73,7 @@ public FirestoreOptions firestoreOptions() {
}

private Credentials emulatorCredentials() {
final Map<String, List<String>> headerMap = new HashMap<>();
headerMap.put("Authorization", Collections.singletonList("Bearer owner"));
headerMap.put("google-cloud-resource-prefix", Collections.singletonList(
rootPath.substring(0, rootPath.lastIndexOf("/documents"))));

return new Credentials() {
@Override
public String getAuthenticationType() {
return null;
}

@Override
public Map<String, List<String>> getRequestMetadata(URI uri) {
return headerMap;
}

@Override
public boolean hasRequestMetadata() {
return true;
}

@Override
public boolean hasRequestMetadataOnly() {
return true;
}

@Override
public void refresh() {
// no-op
}
};
return new FirestoreEmulatorCredentials(this.rootPath);
}

/** Reactive Firestore autoconfiguration to enable emulator use. */
Expand Down
Loading