-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into rewritting-openapi-601
- Loading branch information
Showing
36 changed files
with
1,722 additions
and
106 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
73 changes: 73 additions & 0 deletions
73
core/src/main/java/com/predic8/membrane/core/azure/AzureDns.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,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; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
core/src/main/java/com/predic8/membrane/core/azure/AzureIdentity.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,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; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
core/src/main/java/com/predic8/membrane/core/azure/AzureTableStorage.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,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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
core/src/main/java/com/predic8/membrane/core/azure/api/AzureApiClient.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,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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
core/src/main/java/com/predic8/membrane/core/azure/api/HttpClientConfigurable.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,8 @@ | ||
package com.predic8.membrane.core.azure.api; | ||
|
||
import com.predic8.membrane.core.transport.http.HttpClient; | ||
|
||
public interface HttpClientConfigurable<T> { | ||
HttpClient http(); | ||
T config(); | ||
} |
54 changes: 54 additions & 0 deletions
54
core/src/main/java/com/predic8/membrane/core/azure/api/auth/AuthenticationApi.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,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(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
core/src/main/java/com/predic8/membrane/core/azure/api/dns/DnsProvisionable.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,5 @@ | ||
package com.predic8.membrane.core.azure.api.dns; | ||
|
||
public interface DnsProvisionable { | ||
void provisionDns(String domain, String record); | ||
} |
37 changes: 37 additions & 0 deletions
37
core/src/main/java/com/predic8/membrane/core/azure/api/dns/DnsRecordApi.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,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; | ||
} | ||
} |
Oops, something went wrong.