Skip to content

Commit

Permalink
[Feature] Support aliyun cloud credential (StarRocks#24805)
Browse files Browse the repository at this point in the history
Signed-off-by: miomiocat <284487410@qq.com>
  • Loading branch information
miomiocat authored Jun 8, 2023
1 parent 3897e2f commit 8ea3dbb
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 8 deletions.
19 changes: 19 additions & 0 deletions be/src/fs/credential/cloud_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class AWSCloudCredential final : public CloudCredential {
}
};

class AliyunCloudCredential final : public CloudCredential {
public:
std::string access_key;
std::string secret_key;
std::string endpoint;

bool operator==(const AliyunCloudCredential& rhs) const {
return access_key == rhs.access_key && secret_key == rhs.secret_key && endpoint == rhs.endpoint;
}
};

class CloudConfiguration {
public:
virtual ~CloudConfiguration() = default;
Expand All @@ -56,4 +67,12 @@ class AWSCloudConfiguration final : public CloudConfiguration {
bool enable_path_style_access = false;
bool enable_ssl = true;
};

class AliyunCloudConfiguration final : public CloudConfiguration {
public:
bool operator==(const AliyunCloudConfiguration& rhs) const {
return aliyun_cloud_credential == rhs.aliyun_cloud_credential;
}
AliyunCloudCredential aliyun_cloud_credential;
};
} // namespace starrocks
22 changes: 22 additions & 0 deletions be/src/fs/credential/cloud_configuration_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ static const std::string AWS_S3_ENABLE_PATH_STYLE_ACCESS = "aws.s3.enable_path_s
*/
static const std::string AWS_S3_ENABLE_SSL = "aws.s3.enable_ssl";

static const std::string ALIYUN_OSS_ACCESS_KEY = "aliyun.oss.access_key_id";
static const std::string ALIYUN_OSS_SECRET_KEY = "aliyun.oss.access_key_secret";
static const std::string ALIYUN_OSS_ENDPOINT = "aliyun.oss.endpoint";

class CloudConfigurationFactory {
public:
static const AWSCloudConfiguration create_aws(const TCloudConfiguration& t_cloud_configuration) {
Expand Down Expand Up @@ -80,6 +84,24 @@ class CloudConfigurationFactory {
return aws_cloud_configuration;
}

// This is a reserved interface for aliyun EMR starrocks, and cannot be deleted
static const AliyunCloudConfiguration create_aliyun(const TCloudConfiguration& t_cloud_configuration) {
DCHECK(t_cloud_configuration.__isset.cloud_type);
DCHECK(t_cloud_configuration.cloud_type == TCloudType::ALIYUN);
std::unordered_map<std::string, std::string> properties;
_insert_properties(properties, t_cloud_configuration);

AliyunCloudConfiguration aliyun_cloud_configuration{};
AliyunCloudCredential aliyun_cloud_credential{};

aliyun_cloud_credential.access_key = get_or_default(properties, ALIYUN_OSS_ACCESS_KEY, std::string());
aliyun_cloud_credential.secret_key = get_or_default(properties, ALIYUN_OSS_SECRET_KEY, std::string());
aliyun_cloud_credential.endpoint = get_or_default(properties, ALIYUN_OSS_ENDPOINT, std::string());

aliyun_cloud_configuration.aliyun_cloud_credential = aliyun_cloud_credential;
return aliyun_cloud_configuration;
}

private:
static void _insert_properties(std::unordered_map<std::string, std::string>& properties,
const TCloudConfiguration& t_cloud_configuration) {
Expand Down
4 changes: 0 additions & 4 deletions be/src/fs/fs_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ inline bool is_gcs_uri(std::string_view uri) {
return starts_with(uri, "gs://");
}

inline bool is_jfs_uri(std::string_view uri) {
return starts_with(uri, "jfs://");
}

inline bool is_hdfs_uri(std::string_view uri) {
return starts_with(uri, "hdfs://");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ public class CloudConfigurationConstants {
public static final String HDFS_KERBEROS_PRINCIPAL = "kerberos_principal";
public static final String HDFS_KERBEROS_KEYTAB = "kerberos_keytab";
public static final String HDFS_KERBEROS_KEYTAB_CONTENT = "kerberos_keytab_content";

// Credential for Aliyun OSS
public static final String ALIYUN_OSS_ACCESS_KEY = "aliyun.oss.access_key";
public static final String ALIYUN_OSS_SECRET_KEY = "aliyun.oss.secret_key";
public static final String ALIYUN_OSS_ENDPOINT = "aliyun.oss.endpoint";
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static void maskCloudCredential(Map<String, String> properties) {

// Mask for gcs's credential
doMask(properties, CloudConfigurationConstants.GCP_GCS_SERVICE_ACCOUNT_PRIVATE_KEY);

// Mask for oss's credential
doMask(properties, CloudConfigurationConstants.ALIYUN_OSS_ACCESS_KEY);
doMask(properties, CloudConfigurationConstants.ALIYUN_OSS_SECRET_KEY);
}

private static void doMask(Map<String, String> properties, String configKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public enum CloudType {
AWS,
AZURE,
GCP,
HDFS
HDFS,
ALIYUN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// 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.starrocks.credential.aliyun;

import com.google.common.base.Preconditions;
import com.staros.proto.FileStoreInfo;
import com.starrocks.credential.CloudConfiguration;
import com.starrocks.credential.CloudConfigurationConstants;
import com.starrocks.credential.CloudType;
import com.starrocks.thrift.TCloudConfiguration;
import com.starrocks.thrift.TCloudProperty;
import com.starrocks.thrift.TCloudType;
import org.apache.hadoop.conf.Configuration;

import java.util.LinkedList;
import java.util.List;

public class AliyunCloudConfiguration implements CloudConfiguration {

private final AliyunCloudCredential aliyunCloudCredential;

public AliyunCloudConfiguration(AliyunCloudCredential aliyunCloudCredential) {
Preconditions.checkNotNull(aliyunCloudCredential);
this.aliyunCloudCredential = aliyunCloudCredential;
}

// reuse aws client logic of BE
@Override
public void toThrift(TCloudConfiguration tCloudConfiguration) {
tCloudConfiguration.setCloud_type(TCloudType.AWS);

List<TCloudProperty> properties = new LinkedList<>();
properties.add(new TCloudProperty(CloudConfigurationConstants.AWS_S3_ENABLE_SSL, String.valueOf(true)));
aliyunCloudCredential.toThrift(properties);
tCloudConfiguration.setCloud_properties(properties);
}

@Override
public void applyToConfiguration(Configuration configuration) {
aliyunCloudCredential.applyToConfiguration(configuration);
}

@Override
public CloudType getCloudType() {
return CloudType.ALIYUN;
}

@Override
public FileStoreInfo toFileStoreInfo() {
// TODO: Support oss credential
return aliyunCloudCredential.toFileStoreInfo();
}

@Override
public String getCredentialString() {
return aliyunCloudCredential.getCredentialString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

package com.starrocks.credential.aliyun;

import com.google.common.base.Preconditions;
import com.starrocks.credential.CloudConfiguration;
import com.starrocks.credential.CloudConfigurationFactory;

import java.util.Map;

import static com.starrocks.credential.CloudConfigurationConstants.ALIYUN_OSS_ACCESS_KEY;
import static com.starrocks.credential.CloudConfigurationConstants.ALIYUN_OSS_ENDPOINT;
import static com.starrocks.credential.CloudConfigurationConstants.ALIYUN_OSS_SECRET_KEY;

public class AliyunCloudConfigurationFactory extends CloudConfigurationFactory {
private final Map<String, String> properties;

Expand All @@ -28,7 +33,16 @@ public AliyunCloudConfigurationFactory(Map<String, String> properties) {

@Override
protected CloudConfiguration buildForStorage() {
// TODO
return null;
Preconditions.checkNotNull(properties);

AliyunCloudCredential aliyunCloudCredential = new AliyunCloudCredential(
properties.getOrDefault(ALIYUN_OSS_ACCESS_KEY, ""),
properties.getOrDefault(ALIYUN_OSS_SECRET_KEY, ""),
properties.getOrDefault(ALIYUN_OSS_ENDPOINT, "")
);
if (!aliyunCloudCredential.validate()) {
return null;
}
return new AliyunCloudConfiguration(aliyunCloudCredential);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// 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.starrocks.credential.aliyun;

import com.google.common.base.Preconditions;
import com.staros.proto.FileStoreInfo;
import com.starrocks.credential.CloudConfigurationConstants;
import com.starrocks.credential.CloudCredential;
import com.starrocks.thrift.TCloudProperty;
import org.apache.hadoop.conf.Configuration;

import java.util.List;

public class AliyunCloudCredential implements CloudCredential {

private final String accessKey;
private final String secretKey;
private final String endpoint;

public AliyunCloudCredential(String accessKey, String secretKey, String endpoint) {
Preconditions.checkNotNull(accessKey);
Preconditions.checkNotNull(secretKey);
Preconditions.checkNotNull(endpoint);
this.accessKey = accessKey;
this.secretKey = secretKey;
this.endpoint = endpoint;
}
@Override
public void applyToConfiguration(Configuration configuration) {
configuration.set("fs.oss.impl", "com.aliyun.jindodata.oss.JindoOssFileSystem");
configuration.set("fs.AbstractFileSystem.oss.impl", "com.aliyun.jindodata.oss.OSS");
configuration.set("fs.oss.accessKeyId", accessKey);
configuration.set("fs.oss.accessKeySecret", secretKey);
configuration.set("fs.oss.endpoint", endpoint);
}

@Override
public boolean validate() {
return !this.accessKey.isEmpty() && !this.secretKey.isEmpty() && !this.endpoint.isEmpty();
}

// reuse aws client logic of BE
@Override
public void toThrift(List<TCloudProperty> properties) {
properties.add(new TCloudProperty(CloudConfigurationConstants.AWS_S3_ACCESS_KEY, accessKey));
properties.add(new TCloudProperty(CloudConfigurationConstants.AWS_S3_SECRET_KEY, secretKey));
properties.add(new TCloudProperty(CloudConfigurationConstants.AWS_S3_ENDPOINT, endpoint));
}

@Override
public String getCredentialString() {
return "AliyunCloudCredential{" +
"accessKey=" + accessKey +
", secretKey='" + secretKey + '\'' +
", endpoint='" + endpoint + '\'' +
'}';
}

@Override
public FileStoreInfo toFileStoreInfo() {
// TODO: Support oss credential
return null;
}
}
3 changes: 2 additions & 1 deletion gensrc/thrift/CloudConfiguration.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ enum TCloudType {
DEFAULT,
AWS,
AZURE,
GCP
GCP,
ALIYUN
}

struct TCloudProperty {
Expand Down

0 comments on commit 8ea3dbb

Please sign in to comment.