Skip to content

Commit 2849996

Browse files
lhy0726lhy0726
andauthored
feat: Feature support ksyun (#10)
* feat: support kingsoft cloud * feat: add KINGSOFT_CLOUD rules * fix: fix kcrs resource bug, add all region to DefaultRegions * fix: fix knad HTTP Method, collect networkacls for each vpc * build: add ksyun package step --------- Co-authored-by: lhy0726 <henrylhy0726@163.com>
1 parent e30f82b commit 2849996

155 files changed

Lines changed: 5791 additions & 8 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.application.service.account.cloud.ksyun;
18+
19+
import com.alipay.application.service.account.cloud.Credential;
20+
21+
import java.util.Arrays;
22+
import java.util.List;
23+
24+
/*
25+
*@title KsyunCredential
26+
*@description
27+
*@author jietian
28+
*@version 1.0
29+
*@create 2024/9/19 13:39
30+
*/
31+
public class KsyunCredential implements Credential {
32+
33+
private final String ak;
34+
35+
private final String sk;
36+
37+
public String getAk() {
38+
return ak;
39+
}
40+
41+
public String getSk() {
42+
return sk;
43+
}
44+
45+
public KsyunCredential(String ak, String sk) {
46+
this.ak = ak;
47+
this.sk = sk;
48+
}
49+
50+
@Override
51+
public boolean verification() {
52+
try {
53+
regions();
54+
} catch (Exception e) {
55+
throw new RuntimeException("Cloud account verification failed:" + e.getMessage());
56+
}
57+
return true;
58+
}
59+
60+
@Override
61+
public List<Region> regions() {
62+
return List.of();
63+
}
64+
}

app/application/src/main/java/com/alipay/application/service/account/enums/SecurityProductType.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
public class SecurityProductType {
3737

3838
public static List<String> getSupportedPlatformList() {
39-
return List.of(PlatformType.Enum.ALI_CLOUD, PlatformType.Enum.GCP);
39+
return List.of(PlatformType.Enum.ALI_CLOUD, PlatformType.Enum.GCP, PlatformType.Enum.KINGSOFT_CLOUD);
4040
}
4141

4242
public static final String unknown = "unknown";
@@ -52,6 +52,8 @@ public static List<String> getSecurityProductList(String platform) {
5252
return Arrays.stream(AwsSecurityProductType.values()).map(AwsSecurityProductType::getCode).toList();
5353
} else if (PlatformType.Enum.GCP.equals(platform)) {
5454
return Arrays.stream(GCPSecurityProductType.values()).map(GCPSecurityProductType::getCode).toList();
55+
} else if (PlatformType.Enum.KINGSOFT_CLOUD.equals(platform)) {
56+
return Arrays.stream(KingsoftCloudSecurityProductType.values()).map(KingsoftCloudSecurityProductType::getCode).toList();
5557
}
5658

5759
// ...
@@ -190,4 +192,27 @@ public static GCPSecurityProductType getByCode(String code) {
190192
throw new BizException("暂不支持该产品类型");
191193
}
192194
}
195+
196+
/**
197+
* KINGSOFT_CLOUD安全产品类型枚举
198+
*/
199+
@Getter
200+
@AllArgsConstructor
201+
public enum KingsoftCloudSecurityProductType {
202+
DDOS("DDoS", "DDoS 高防", "KNAD"),
203+
FIREWALL("FIRE WALL", "防火墙", "FIREWALL"),
204+
WAF("WAF", "WAF", "WAF");
205+
private final String code;
206+
private final String desc;
207+
private final String relatedResourceType;
208+
209+
public static KingsoftCloudSecurityProductType getByCode(String code) {
210+
for (KingsoftCloudSecurityProductType value : values()) {
211+
if (value.getCode().equals(code)) {
212+
return value;
213+
}
214+
}
215+
throw new BizException("暂不支持该产品类型");
216+
}
217+
}
193218
}

app/application/src/main/java/com/alipay/application/service/account/utils/PlatformUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.alipay.application.service.account.cloud.hws.HwsCredential;
2727
import com.alipay.application.service.account.cloud.hwsprivate.HwsPrivateCredential;
2828
import com.alipay.application.service.account.cloud.tencent.TencentCredential;
29+
import com.alipay.application.service.account.cloud.ksyun.KsyunCredential;
2930
import com.alipay.common.constant.MarkConstants;
3031
import com.alipay.common.enums.PlatformType;
3132
import com.google.gson.Gson;
@@ -104,6 +105,10 @@ public static Map<String, String> getAccountCredentialsInfo(String platform, Str
104105
break;
105106
// case PlatformType.Enum.My_Cloud_Provider:
106107
// [3] ADD_NEW_CLOUD : To adapt the logic of parsing authentication information, you need to add a class corresponding to the cloud platform authentication information
108+
case PlatformType.Enum.KINGSOFT_CLOUD:
109+
KsyunCredential ksyunCredential = new Gson().fromJson(credentialsJson, KsyunCredential.class);
110+
map.put("ak", ksyunCredential.getAk());
111+
map.put("sk", ksyunCredential.getSk());
107112
default:
108113
throw new IllegalStateException("Unexpected value: " + platform);
109114
}

app/application/src/main/java/com/alipay/application/service/collector/AgentServiceImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,14 @@ private OnceTokenVO getOnceTokenResult(UserPO userPO, AgentRegistryTokenPO exist
285285
String alibabCloudPrivateScript = parseScript(scriptTemplate, "deploy_alicloud_private", "cloudrec_collector_alicloud_private", bucketUrl, serverUrl, existPO.getOnceToken());
286286
result.add(createOnceToken(Platform.getPlatformName(PlatformType.ALI_CLOUD_PRIVATE.getPlatform()), alibabCloudPrivateScript, userPO, existPO));
287287

288+
// ksyun account
289+
String ksyunScript = parseScript(scriptTemplate, "deploy_ksyun", "cloudrec_collector_ksyun", bucketUrl, serverUrl, existPO.getOnceToken());
290+
result.add(createOnceToken(Platform.getPlatformName(PlatformType.KINGSOFT_CLOUD.getPlatform()), ksyunScript, userPO, existPO));
291+
288292
// all account platforms
289293
String cloudRecScript = parseScript(scriptTemplate, "deploy_cloudrec", "cloudrec_collector", bucketUrl, serverUrl, existPO.getOnceToken());
290294
List<String> platformList = Platform.getPlatformNameList(PlatformType.ALI_CLOUD, PlatformType.TENCENT_CLOUD,
291-
PlatformType.BAIDU_CLOUD, PlatformType.HUAWEI_CLOUD, PlatformType.TENCENT_CLOUD, PlatformType.BAIDU_CLOUD);
295+
PlatformType.BAIDU_CLOUD, PlatformType.HUAWEI_CLOUD, PlatformType.TENCENT_CLOUD, PlatformType.BAIDU_CLOUD, PlatformType.KINGSOFT_CLOUD);
292296
result.add(createOnceToken(Strings.join(platformList, ','), cloudRecScript, userPO, existPO));
293297

294298
OnceTokenVO onceToken = createOnceToken(Strings.join(platformList, ','), cloudRecScript, userPO, existPO);

app/application/src/main/java/com/alipay/application/service/resource/enums/IdentitySecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public enum IdentitySecurityConfig {
3434
ALI_CLOUD_RAM_User(PlatformType.Enum.ALI_CLOUD, "RAM User", "User"),
3535
HUAWEI_CLOUD_IAM_User(PlatformType.Enum.HUAWEI_CLOUD, "IAM User", "User"),
3636
GCP_IAM_ServiceAccount(PlatformType.Enum.GCP, "IAM ServiceAccount","Service Account"),
37+
KINGSOFT_CLOUD_IAM_User(PlatformType.Enum.KINGSOFT_CLOUD, "IAM User","User"),
3738
;
3839

3940
private String platformType;

app/application/src/main/java/com/alipay/application/service/resource/identitySecurity/IdentitySecurityServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public List<IdentitySecurityRiskInfoVO> queryRiskInfo(QueryIdentityRuleRequest r
174174
@Override
175175
public List<PlatformPO> getPlatformList() {
176176
List<PlatformPO> res = new ArrayList<>();
177-
List<String> platformList = List.of(PlatformType.Enum.ALI_CLOUD, PlatformType.Enum.GCP, PlatformType.HUAWEI_CLOUD.getPlatform());
177+
List<String> platformList = List.of(PlatformType.Enum.ALI_CLOUD, PlatformType.Enum.GCP, PlatformType.HUAWEI_CLOUD.getPlatform(), PlatformType.Enum.KINGSOFT_CLOUD);
178178
for (String platform : platformList) {
179179
PlatformPO platformPO = platformMapper.findByPlatform(platform);
180180
if (platformPO != null) {
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.application.service.resource.identitySecurity;
18+
19+
import com.alibaba.fastjson.JSON;
20+
import com.alibaba.fastjson.JSONObject;
21+
import com.alipay.application.service.resource.enums.IdentitySecurityConfig;
22+
import com.alipay.application.service.resource.enums.ResourceVisitType;
23+
import com.alipay.application.service.resource.identitySecurity.model.ResourceAccessInfoDTO;
24+
import com.alipay.application.service.resource.identitySecurity.model.ResourcePolicyInfoDTO;
25+
import com.alipay.application.service.resource.identitySecurity.model.ResourceUserInfoDTO;
26+
import com.alipay.dao.po.IdentitySecurityPO;
27+
import org.apache.commons.lang3.StringUtils;
28+
import org.apache.commons.lang3.BooleanUtils;
29+
import org.springframework.stereotype.Service;
30+
31+
import java.util.*;
32+
33+
/**
34+
* Date: 2025/4/28
35+
* Author: lz
36+
*/
37+
@Service
38+
public class KsyunIamUserResourceParse implements CloudResourceInfoParser{
39+
40+
@Override
41+
public IdentitySecurityPO parse(IdentitySecurityPO identitySecurityPO, String resourceInstance) {
42+
identitySecurityPO.setResourceTypeGroup(IdentitySecurityConfig.KINGSOFT_CLOUD_IAM_User.getResourceTypeGroup());
43+
identitySecurityPO.setPlatform(IdentitySecurityConfig.KINGSOFT_CLOUD_IAM_User.getPlatformType());
44+
identitySecurityPO.setAccessType(String.join(",", getVisitTypes(resourceInstance)));
45+
identitySecurityPO.setAccessInfos(JSON.toJSONString(parseAccessInfo(resourceInstance)));
46+
identitySecurityPO.setUserInfo(JSON.toJSONString(parseUserInfo(resourceInstance)));
47+
identitySecurityPO.setPolicies(JSON.toJSONString(parsePolicyInfo(resourceInstance)));
48+
return identitySecurityPO;
49+
}
50+
51+
@Override
52+
public ResourceUserInfoDTO parseUserInfo(String resourceInstance) {
53+
JSONObject jsonObject = coverResourceInstanceStr(resourceInstance);
54+
ResourceUserInfoDTO resourceUserInfoDTO = new ResourceUserInfoDTO();
55+
if(jsonObject.get("User") != null){
56+
JSONObject userAttribute = jsonObject.getJSONObject("User");
57+
resourceUserInfoDTO.setUserName(userAttribute.getString("UserName"));
58+
resourceUserInfoDTO.setUserId(userAttribute.getString("UserId"));
59+
resourceUserInfoDTO.setCreateDate(userAttribute.getString("CreateDate"));
60+
resourceUserInfoDTO.setUpdateDate(userAttribute.getString("UpdateDate"));
61+
resourceUserInfoDTO.setEmail(userAttribute.getString("Email"));
62+
}
63+
return resourceUserInfoDTO;
64+
}
65+
66+
@Override
67+
public List<ResourcePolicyInfoDTO> parsePolicyInfo(String resourceInstance) {
68+
return null;
69+
}
70+
71+
@Override
72+
public List<ResourceAccessInfoDTO> parseAccessInfo(String resourceInstance) {
73+
JSONObject jsonObject = coverResourceInstanceStr(resourceInstance);
74+
List<ResourceAccessInfoDTO> resourceAccessInfoDTOS = new ArrayList<>();
75+
Object accessKeys = jsonObject.get("AccessKeys");
76+
if (accessKeys != null) {
77+
List<JSONObject> accessKeyList = JSON.parseArray(JSON.toJSONString(accessKeys), JSONObject.class);
78+
for (JSONObject accessKey : accessKeyList) {
79+
ResourceAccessInfoDTO resourceAccessInfoDTO = new ResourceAccessInfoDTO();
80+
resourceAccessInfoDTO.setAccessKeyId(accessKey.getString("AccessKeyId"));
81+
resourceAccessInfoDTOS.add(resourceAccessInfoDTO);
82+
}
83+
}
84+
return resourceAccessInfoDTOS;
85+
}
86+
87+
@Override
88+
public List<String> getVisitTypes(String resourceInstance) {
89+
JSONObject jsonObject = coverResourceInstanceStr(resourceInstance);
90+
List<String> visitTypes = new ArrayList<>();
91+
Object accessKeys = jsonObject.get("AccessKeys");
92+
if (accessKeys != null) {
93+
List<JSONObject> accessKeyList = JSON.parseArray(JSON.toJSONString(accessKeys), JSONObject.class);
94+
if (accessKeyList.size() > 0) {
95+
visitTypes.add(ResourceVisitType.API.name());
96+
}
97+
}
98+
99+
Object loginProfile = jsonObject.get("LoginProfile");
100+
if (loginProfile != null && BooleanUtils.isTrue(BooleanUtils.toBoolean(jsonObject.getString("ConsoleLogin")))) {
101+
visitTypes.add(ResourceVisitType.Console.name());
102+
}
103+
104+
return visitTypes;
105+
}
106+
107+
@Override
108+
public List<String> parseTags(String resourceInstance, String ruleIds, String cloudAccountId, String resourceId) {
109+
return null;
110+
}
111+
112+
113+
private JSONObject coverResourceInstanceStr(String resourceInstanceStr) {
114+
if (StringUtils.isBlank(resourceInstanceStr)) {
115+
return new JSONObject();
116+
}
117+
return JSON.parseObject(resourceInstanceStr);
118+
}
119+
}

app/application/src/main/java/com/alipay/application/service/statistics/job/ParseCloudResourceDataJob.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.alipay.application.service.resource.identitySecurity.AliRamUserResourceParse;
2323
import com.alipay.application.service.resource.identitySecurity.GCPServiceAccountParse;
2424
import com.alipay.application.service.resource.identitySecurity.HuaweiIamUserResourceParse;
25+
import com.alipay.application.service.resource.identitySecurity.KsyunIamUserResourceParse;
2526
import com.alipay.application.service.risk.RiskService;
2627
import com.alipay.application.service.rule.enums.RuleType;
2728
import com.alipay.application.share.vo.ApiResponse;
@@ -66,6 +67,9 @@ public class ParseCloudResourceDataJob {
6667
private HuaweiIamUserResourceParse huaweiIamUserResourceParse;
6768

6869
@Resource
70+
private KsyunIamUserResourceParse ksyunIamUserResourceParse;
71+
72+
@Resource
6973
private RuleScanResultMapper ruleScanResultMapper;
7074

7175
@Resource
@@ -167,6 +171,9 @@ private IdentitySecurityPO buildIdentitySecurityParam(IdentitySecurityConfig con
167171
if (Objects.equals(config, IdentitySecurityConfig.HUAWEI_CLOUD_IAM_User)) {
168172
huaweiIamUserResourceParse.parse(identitySecurityPO, resourceInstance);
169173
}
174+
if (Objects.equals(config, IdentitySecurityConfig.KINGSOFT_CLOUD_IAM_User)) {
175+
ksyunIamUserResourceParse.parse(identitySecurityPO, resourceInstance);
176+
}
170177
return identitySecurityPO;
171178
}
172179

app/common/src/main/java/com/alipay/common/enums/PlatformType.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ public enum PlatformType {
4242

4343
GCP(Enum.GCP, "GCP", "GCP"),
4444

45-
AZURE(Enum.AZURE, "AZURE", "AZURE");
45+
AZURE(Enum.AZURE, "AZURE", "AZURE"),
4646
// [2] ADD_NEW_CLOUD : Add a new cloud provider.
4747
// MyCloudProvider(Enum.MyCloudProvider, "我的云", "My Cloud Provider");
4848

49+
KINGSOFT_CLOUD(Enum.KINGSOFT_CLOUD, "金山云", "Kingsoft Cloud");
50+
4951
private String platform;
5052
private String cnName;
5153

@@ -109,5 +111,6 @@ public static class Enum {
109111
public static final String AZURE = "AZURE";
110112
// [1] ADD_NEW_CLOUD : Add a new cloud provider enum.
111113
// public static final String MyCloudProvider = "My_Cloud_Provider";
114+
public static final String KINGSOFT_CLOUD = "KINGSOFT_CLOUD";
112115
}
113116
}
15.5 KB
Loading

0 commit comments

Comments
 (0)