forked from apache/inlong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-10387][Audit] Audit SDK supports obtaining Audit-Proxy confid…
…ence through the InLong Manager
- Loading branch information
Showing
13 changed files
with
387 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
inlong-audit/audit-common/src/main/java/org/apache/inlong/audit/entity/AuthConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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 org.apache.inlong.audit.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
/** | ||
* Class representing an authentication configuration. | ||
*/ | ||
@AllArgsConstructor | ||
@Data | ||
public class AuthConfig { | ||
|
||
/** | ||
* The secret ID for authentication. | ||
*/ | ||
private String secretId; | ||
|
||
/** | ||
* The secret key for authentication. | ||
*/ | ||
private String secretKey; | ||
|
||
/** | ||
* The token for authentication. | ||
*/ | ||
private String token; | ||
|
||
/** | ||
* The service name for authentication. | ||
*/ | ||
private String serviceName; | ||
} |
91 changes: 91 additions & 0 deletions
91
inlong-audit/audit-common/src/main/java/org/apache/inlong/audit/entity/CommonResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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 org.apache.inlong.audit.entity; | ||
|
||
import com.google.gson.Gson; | ||
import lombok.Data; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
/** | ||
* Class representing a common response. | ||
*/ | ||
@Data | ||
public class CommonResponse<T> { | ||
|
||
/** | ||
* Gson instance for JSON serialization and deserialization. | ||
*/ | ||
private static final Gson gson = new Gson(); | ||
|
||
/** | ||
* Error message of the response. | ||
*/ | ||
private String errMsg; | ||
|
||
/** | ||
* Success status of the response. | ||
*/ | ||
private boolean success; | ||
|
||
/** | ||
* Data of the response. | ||
*/ | ||
private List<T> data; | ||
|
||
/** | ||
* Converts a JSON string to a CommonResponse object. | ||
* | ||
* @param json the JSON string | ||
* @param clazz the class of the data | ||
* @return a CommonResponse object | ||
*/ | ||
public static CommonResponse fromJson(String json, Class clazz) { | ||
Type objectType = type(CommonResponse.class, clazz); | ||
return gson.fromJson(json, objectType); | ||
} | ||
|
||
/** | ||
* Returns a parameterized type. | ||
* | ||
* @param raw the raw type | ||
* @param args the actual type arguments | ||
* @return a parameterized type | ||
*/ | ||
private static ParameterizedType type(final Class raw, final Type... args) { | ||
return new ParameterizedType() { | ||
|
||
@Override | ||
public Type getRawType() { | ||
return raw; | ||
} | ||
|
||
@Override | ||
public Type[] getActualTypeArguments() { | ||
return args; | ||
} | ||
|
||
@Override | ||
public Type getOwnerType() { | ||
return null; | ||
} | ||
}; | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
inlong-audit/audit-common/src/main/java/org/apache/inlong/audit/utils/HttpUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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 org.apache.inlong.audit.utils; | ||
|
||
import org.apache.inlong.audit.entity.AuthConfig; | ||
import org.apache.inlong.common.util.BasicAuth; | ||
|
||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.utils.URIBuilder; | ||
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.ssl.SSLContextBuilder; | ||
import org.apache.http.util.EntityUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.net.ssl.SSLContext; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class HttpUtils { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class); | ||
private static final String PARAM_COMPONENT = "component"; | ||
private static HttpClient httpClient = null; | ||
|
||
static { | ||
try { | ||
SSLContext sslContext = SSLContextBuilder.create() | ||
.loadTrustMaterial(new TrustSelfSignedStrategy()) | ||
.build(); | ||
|
||
httpClient = HttpClientBuilder.create() | ||
.setSSLContext(sslContext) | ||
.build(); | ||
} catch (Exception e) { | ||
LOGGER.error("Error initializing SSL context or HTTP client", e); | ||
} | ||
} | ||
|
||
public static Map<String, String> getAuthHeader(AuthConfig authConfig) { | ||
Map<String, String> header = new HashMap<>(); | ||
try { | ||
header.put(BasicAuth.BASIC_AUTH_HEADER, | ||
BasicAuth.genBasicAuthCredential(authConfig.getSecretId(), authConfig.getSecretKey())); | ||
} catch (Exception e) { | ||
LOGGER.error("Get auth header error", e); | ||
} | ||
return header; | ||
} | ||
public static String sendGet(String component, String url, AuthConfig authConfig, int timeoutMs) { | ||
if (httpClient == null) { | ||
LOGGER.error("httpClient is null"); | ||
return null; | ||
} | ||
try { | ||
RequestConfig requestConfig = RequestConfig.custom() | ||
.setConnectTimeout(timeoutMs) | ||
.setConnectionRequestTimeout(timeoutMs) | ||
.setSocketTimeout(timeoutMs) | ||
.build(); | ||
URIBuilder uriBuilder = new URIBuilder(url); | ||
uriBuilder.addParameter(PARAM_COMPONENT, component); | ||
String finalUrl = uriBuilder.build().toString(); | ||
|
||
HttpGet request = new HttpGet(finalUrl); | ||
request.setConfig(requestConfig); | ||
|
||
Map<String, String> authHeaders = getAuthHeader(authConfig); | ||
for (Map.Entry<String, String> entry : authHeaders.entrySet()) { | ||
request.addHeader(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
try (CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(request)) { | ||
String responseStr = EntityUtils.toString(response.getEntity()); | ||
LOGGER.info("Http response: {}", responseStr); | ||
if (responseStr != null && !responseStr.isEmpty() | ||
&& response.getStatusLine().getStatusCode() == 200) { | ||
return responseStr; | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Send get request has exception", e); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.