Skip to content

Commit

Permalink
Merge branch 'master' into rewritting-openapi-601
Browse files Browse the repository at this point in the history
  • Loading branch information
predic8 authored Jul 19, 2023
2 parents c87f11d + ed7e7a4 commit 6afffd5
Show file tree
Hide file tree
Showing 36 changed files with 1,722 additions and 106 deletions.
3 changes: 1 addition & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -432,4 +431,4 @@
</plugins>
</reporting>

</project>
</project>
73 changes: 73 additions & 0 deletions core/src/main/java/com/predic8/membrane/core/azure/AzureDns.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.predic8.membrane.core.azure;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.core.config.security.acme.AcmeValidation;

@MCElement(topLevel = false, name = "azureDns")
public class AzureDns extends AcmeValidation {

private String dnsZoneName;
private String subscriptionId;
private String tenantId;
private String resourceGroup;
private String resource = "https://management.azure.com";
private AzureIdentity identity;

public String getDnsZoneName() {
return dnsZoneName;
}

@MCAttribute
public void setDnsZoneName(String dnsZoneName) {
this.dnsZoneName = dnsZoneName;
}

public String getSubscriptionId() {
return subscriptionId;
}

@MCAttribute
public void setSubscriptionId(String subscriptionId) {
this.subscriptionId = subscriptionId;
}

public String getTenantId() {
if (identity != null) {
return identity.getTenantId();
}
return tenantId;
}

@MCAttribute
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}

public String getResourceGroup() {
return resourceGroup;
}

@MCAttribute
public void setResourceGroup(String resourceGroup) {
this.resourceGroup = resourceGroup;
}

public String getResource() {
return resource;
}

@MCAttribute
public void setResource(String resource) {
this.resource = resource;
}

public AzureIdentity getIdentity() {
return identity;
}

@MCAttribute
public void setIdentity(AzureIdentity identity) {
this.identity = identity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.predic8.membrane.core.azure;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;

@MCElement(name = "azureIdentity")
public class AzureIdentity {

private String grantType = "client_credentials";
private String clientId;
private String clientSecret;
private String resource = "https://management.azure.com";
private String tenantId;

public String getGrantType() {
return grantType;
}

@MCAttribute
public void setGrantType(String grantType) {
this.grantType = grantType;
}

public String getClientId() {
return clientId;
}

@MCAttribute
public void setClientId(String clientId) {
this.clientId = clientId;
}

public String getClientSecret() {
return clientSecret;
}

@MCAttribute
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}

public String getResource() {
return resource;
}

@MCAttribute
public void setResource(String resource) {
this.resource = resource;
}

public String getTenantId() {
return tenantId;
}

@MCAttribute
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.predic8.membrane.core.azure;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCChildElement;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.core.config.security.acme.AcmeSynchronizedStorage;
import com.predic8.membrane.core.transport.http.client.HttpClientConfiguration;

@MCElement(name = "azureTableStorage", topLevel = false)
public class AzureTableStorage implements AcmeSynchronizedStorage {

private String storageAccountName;
private String storageAccountKey;
private String tableName = "membrane";
private String partitionKey = "acme";
private HttpClientConfiguration httpClientConfiguration;

private String customHost;

public String getCustomHost() {
return customHost;
}

public void setCustomHost(String customHost) {
this.customHost = customHost;
}

public String getStorageAccountName() {
return storageAccountName;
}

@MCAttribute
public void setStorageAccountName(String storageAccountName) {
this.storageAccountName = storageAccountName;
}

public String getStorageAccountKey() {
return storageAccountKey;
}

@MCAttribute
public void setStorageAccountKey(String storageAccountKey) {
this.storageAccountKey = storageAccountKey;
}

public String getTableName() {
return tableName;
}

@MCAttribute
public void setTableName(String tableName) {
this.tableName = tableName;
}

public String getPartitionKey() {
return partitionKey;
}

@MCAttribute
public void setPartitionKey(String partitionKey) {
this.partitionKey = partitionKey;
}

public HttpClientConfiguration getHttpClientConfiguration() {
return httpClientConfiguration;
}

@MCChildElement
public void setHttpClientConfiguration(HttpClientConfiguration httpClientConfiguration) {
this.httpClientConfiguration = httpClientConfiguration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.predic8.membrane.core.azure.api;

import com.predic8.membrane.core.azure.AzureDns;
import com.predic8.membrane.core.azure.AzureIdentity;
import com.predic8.membrane.core.azure.AzureTableStorage;
import com.predic8.membrane.core.azure.api.auth.AuthenticationApi;
import com.predic8.membrane.core.azure.api.dns.DnsRecordApi;
import com.predic8.membrane.core.azure.api.tablestorage.TableStorageApi;
import com.predic8.membrane.core.transport.http.HttpClient;
import com.predic8.membrane.core.transport.http.HttpClientFactory;
import com.predic8.membrane.core.util.TimerManager;

import javax.annotation.Nullable;

public class AzureApiClient implements AutoCloseable {

private final HttpClient httpClient;
private final AuthenticationApi authApi;
private final TableStorageApi tableStorageApi;


public AzureApiClient(
@Nullable AzureIdentity identityConfig,
AzureTableStorage tableStorage,
HttpClientFactory httpClientFactory
) {
if (httpClientFactory == null) {
httpClientFactory = new HttpClientFactory(new TimerManager());
}
this.httpClient = httpClientFactory.createClient(tableStorage.getHttpClientConfiguration());

authApi = new AuthenticationApi(httpClient, identityConfig);
tableStorageApi = new TableStorageApi(this, tableStorage);
}

public DnsRecordApi dnsRecords(AzureDns dnsOperator) {
return new DnsRecordApi(this, dnsOperator);
}

public TableStorageApi tableStorage() {
return tableStorageApi;
}

public AuthenticationApi auth() {
return authApi;
}

public HttpClient httpClient() {
return httpClient;
}

@Override
public void close() throws Exception {
this.httpClient.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.predic8.membrane.core.azure.api;

import com.predic8.membrane.core.transport.http.HttpClient;

public interface HttpClientConfigurable<T> {
HttpClient http();
T config();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.predic8.membrane.core.azure.api.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.predic8.membrane.core.azure.AzureIdentity;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.Request;
import com.predic8.membrane.core.transport.http.HttpClient;

import javax.annotation.Nullable;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.stream.Collectors;

public class AuthenticationApi {

private final HttpClient http;
private final AzureIdentity config;
private final Map<String, String> tokenPayload;

public AuthenticationApi(HttpClient http, @Nullable AzureIdentity config) {
this.http = http;
this.config = config;

if (config == null) {
tokenPayload = Map.of();
return;
}

tokenPayload = Map.of(
"grant_type", config.getGrantType(),
"client_id", config.getClientId(),
"client_secret", config.getClientSecret(),
"resource", config.getResource()
);
}

public String accessToken() throws Exception {
var response = http.call(tokenExchange()).getResponse();
return new ObjectMapper()
.readTree(response.getBodyAsStringDecoded())
.get("access_token")
.asText();
}

private Exchange tokenExchange() throws URISyntaxException {
var tenantId = config.getTenantId();
return new Request.Builder()
.post("https://login.microsoftonline.com/" + tenantId + "/oauth2/token")
.body(tokenPayload.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&")))
.buildExchange();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.predic8.membrane.core.azure.api.dns;

public interface DnsProvisionable {
void provisionDns(String domain, String record);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.predic8.membrane.core.azure.api.dns;

import com.predic8.membrane.core.azure.AzureDns;
import com.predic8.membrane.core.azure.api.AzureApiClient;
import com.predic8.membrane.core.azure.api.HttpClientConfigurable;
import com.predic8.membrane.core.http.Request;
import com.predic8.membrane.core.transport.http.HttpClient;

public class DnsRecordApi implements HttpClientConfigurable<AzureDns> {

private final AzureApiClient apiClient;
private final AzureDns config;

public DnsRecordApi(AzureApiClient apiClient, AzureDns config) {
this.apiClient = apiClient;
this.config = config;
}

public DnsRecordCommandExecutor txt(String name) {
return new DnsRecordCommandExecutor(this, name, DnsRecordType.TXT);
}

protected Request.Builder requestBuilder() throws Exception {
return new Request.Builder()
.header("Authorization", "Bearer " + apiClient.auth().accessToken());
}

@Override
public HttpClient http() {
return apiClient.httpClient();
}

@Override
public AzureDns config() {
return config;
}
}
Loading

0 comments on commit 6afffd5

Please sign in to comment.