Skip to content

Do not rely on default os endpoint for AWS S3 #1593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
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
Expand Up @@ -29,6 +29,7 @@ private Constants() {
public static final String CONTAINER_NAME = "container_name";
public static final String CONTAINER_URI = "container_uri";
public static final String BASE_64_ENCODED_PRIVATE_KEY_DATA = "base64EncodedPrivateKeyData";
public static final String HOST = "host";

public static final String AWS_S_3 = "aws-s3";
public static final String AZUREBLOB = "azureblob";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.cloudfoundry.multiapps.controller.web.configuration.bean.factory;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.pivotal.cfenv.core.CfService;
import org.apache.commons.lang3.StringUtils;
import org.cloudfoundry.multiapps.controller.core.util.UriUtil;
import org.cloudfoundry.multiapps.controller.persistence.services.ObjectStoreFileStorage;
import org.cloudfoundry.multiapps.controller.persistence.util.EnvironmentServicesFinder;
import org.cloudfoundry.multiapps.controller.web.Messages;
Expand All @@ -18,7 +15,11 @@
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import io.pivotal.cfenv.core.CfService;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ObjectStoreFileStorageFactoryBean implements FactoryBean<ObjectStoreFileStorage>, InitializingBean {

Expand Down Expand Up @@ -60,10 +61,9 @@ private ObjectStoreFileStorage createObjectStoreFileStorage() {
exceptions.put(objectStoreServiceInfo.getProvider(), e);
}
}
exceptions.forEach((provider,
exception) -> LOGGER.error(MessageFormat.format(Messages.CANNOT_CREATE_OBJECT_STORE_CLIENT_WITH_PROVIDER_0,
provider),
exception));
exceptions.forEach(
(provider, exception) -> LOGGER.error(MessageFormat.format(Messages.CANNOT_CREATE_OBJECT_STORE_CLIENT_WITH_PROVIDER_0, provider),
exception));
throw new IllegalStateException(Messages.NO_VALID_OBJECT_STORE_CONFIGURATION_FOUND);
}

Expand All @@ -84,10 +84,18 @@ private BlobStoreContext getBlobStoreContext(ObjectStoreServiceInfo serviceInfo)
} else {
return null;
}
if (serviceInfo.getEndpoint() != null) {
resolveContextEndpoint(serviceInfo, contextBuilder);
return contextBuilder.buildView(BlobStoreContext.class);
}

private void resolveContextEndpoint(ObjectStoreServiceInfo serviceInfo, ContextBuilder contextBuilder) {
if (StringUtils.isNotEmpty(serviceInfo.getEndpoint())) {
contextBuilder.endpoint(serviceInfo.getEndpoint());
return;
}
if (StringUtils.isNotEmpty(serviceInfo.getHost())) {
contextBuilder.endpoint(UriUtil.HTTPS_PROTOCOL + UriUtil.DEFAULT_SCHEME_SEPARATOR + serviceInfo.getHost());
}
return contextBuilder.buildView(BlobStoreContext.class);
}

protected ObjectStoreFileStorage createFileStorage(ObjectStoreServiceInfo objectStoreServiceInfo, BlobStoreContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.cloudfoundry.multiapps.controller.web.configuration.service;

import com.google.common.base.Supplier;
import org.cloudfoundry.multiapps.common.Nullable;
import org.immutables.value.Value;
import org.jclouds.domain.Credentials;

import com.google.common.base.Supplier;

@Value.Immutable
public interface ObjectStoreServiceInfo {

Expand All @@ -29,4 +28,7 @@ public interface ObjectStoreServiceInfo {
@Nullable
String getRegion();

@Nullable
String getHost();

}
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package org.cloudfoundry.multiapps.controller.web.configuration.service;

import com.google.common.base.Supplier;
import io.pivotal.cfenv.core.CfService;
import org.cloudfoundry.multiapps.controller.web.Constants;
import org.cloudfoundry.multiapps.controller.web.Messages;
import org.jclouds.domain.Credentials;
import org.jclouds.googlecloud.GoogleCredentialsFromJson;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import org.cloudfoundry.multiapps.controller.web.Constants;
import org.cloudfoundry.multiapps.controller.web.Messages;
import org.jclouds.domain.Credentials;
import org.jclouds.googlecloud.GoogleCredentialsFromJson;

import com.google.common.base.Supplier;

import io.pivotal.cfenv.core.CfService;

public class ObjectStoreServiceInfoCreator {

public List<ObjectStoreServiceInfo> getAllProvidersServiceInfo(CfService service) {
Map<String, Object> credentials = service.getCredentials()
.getMap();
return List.of(createServiceInfoForAws(credentials), createServiceInfoForAliCloud(credentials),
createServiceInfoForAzure(credentials), createServiceInfoForGcpCloud(credentials));
return List.of(createServiceInfoForAws(credentials), createServiceInfoForAliCloud(credentials), createServiceInfoForAzure(credentials),
createServiceInfoForGcpCloud(credentials));
}

private ObjectStoreServiceInfo createServiceInfoForAws(Map<String, Object> credentials) {
String accessKeyId = (String) credentials.get(Constants.ACCESS_KEY_ID);
String secretAccessKey = (String) credentials.get(Constants.SECRET_ACCESS_KEY);
String bucket = (String) credentials.get(Constants.BUCKET);
String host = (String) credentials.get(Constants.HOST);
return ImmutableObjectStoreServiceInfo.builder()
.provider(Constants.AWS_S_3)
.identity(accessKeyId)
.credential(secretAccessKey)
.container(bucket)
.host(host)
.build();
}

Expand Down