Skip to content

Commit

Permalink
Merge pull request #86 from baoduy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
baoduy authored Sep 19, 2024
2 parents 27977e0 + 0956513 commit 203296d
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 70 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"@pulumi/azure-native": "^2.59.0",
"@pulumi/azuread": "5.53.3",
"@pulumi/pulumi": "^3.131.0",
"@pulumi/random": "^4.16.3",
"@pulumi/tls": "^5.0.4",
"@pulumi/random": "^4.16.4",
"@pulumi/tls": "^5.0.5",
"netmask": "^2.0.2",
"node-forge": "^1.3.1",
"to-words": "^4.1.0"
Expand Down
8 changes: 2 additions & 6 deletions src/AzAd/Identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import {
ApplicationOptionalClaims,
ApplicationRequiredResourceAccess,
} from '@pulumi/azuread/types/input';
import {
WithNamedType,
NamedWithVaultBasicArgs,
AdIdentityInfoWithInstance,
} from '../types';
import { addCustomSecret, addCustomSecrets } from '../KeyVault/CustomHelper';
import { NamedWithVaultBasicArgs, AdIdentityInfoWithInstance } from '../types';
import { addCustomSecret, addCustomSecrets } from '../KeyVault';
import { getIdentitySecretNames } from './Helper';

type PreAuthApplicationProps = {
Expand Down
31 changes: 30 additions & 1 deletion src/Builder/ApimApiBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as apim from '@pulumi/azure-native/apimanagement';
import { enums } from '@pulumi/azure-native/types';
import { Input, interpolate } from '@pulumi/pulumi';
import { openApi } from '../Common';
import { openApi, subscriptionId } from '../Common';
import { ResourceInfo, WithDependsOn } from '../types';
import ApimPolicyBuilder from './ApimPolicyBuilder';
import {
Expand Down Expand Up @@ -162,6 +162,32 @@ export default class ApimApiBuilder
});
}

private buildApiDiagnostic({
apiId,
dependsOn,
}: { apiId: string } & WithDependsOn) {
new apim.ApiDiagnostic(
`apim-${apiId}-apiDiagnostic`,
{
serviceName: this.args.apimServiceName,
resourceGroupName: this.args.group.resourceGroupName,
apiId,
alwaysLog: apim.AlwaysLog.AllErrors,
httpCorrelationProtocol: 'W3C',
operationNameFormat: 'Url',
logClientIp: true,
verbosity: 'information',
loggerId: interpolate`/subscriptions/${subscriptionId}/resourceGroups/${this.args.group.resourceGroupName}/providers/Microsoft.ApiManagement/service/${this.args.apimServiceName}/loggers/${this.args.apimServiceName}-appInsight`,
diagnosticId: 'applicationinsights',
sampling: {
percentage: 100,
samplingType: apim.SamplingType.Fixed,
},
},
{ dependsOn },
);
}

private async buildApis() {
const date = new Date();
const tasks = Object.keys(this._apis).map(async (v) => {
Expand Down Expand Up @@ -237,6 +263,9 @@ export default class ApimApiBuilder
);
}

//Diagnostic
this.buildApiDiagnostic({ apiId: apiName, dependsOn: api });

//Create Aoi Operations
if ('operations' in apiProps) {
this.buildOperations({
Expand Down
76 changes: 58 additions & 18 deletions src/Builder/ApimBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as types from './types';
import { EnvRoleKeyTypes, ResourceInfo } from '../types';
import * as apim from '@pulumi/azure-native/apimanagement';
import { getSecretOutput, addCustomSecret } from '../KeyVault';
import { naming, organization, subscriptionId, tenantId } from '../Common';
import {
ApimSignInSettingsResource,
Expand Down Expand Up @@ -93,6 +94,15 @@ class ApimBuilder
this._proxyDomain = props;
return this;
}
public withProxyDomainIf(
condition: boolean,
props: types.ApimDomainBuilderType,
): types.IApimBuilder {
if (condition) {
this.withProxyDomain(props);
}
return this;
}
public withPublisher(
props: types.ApimPublisherBuilderType,
): types.IApimBuilder {
Expand Down Expand Up @@ -128,8 +138,24 @@ class ApimBuilder
});
}
}

private getCert(props: types.ApimCertBuilderType) {
if ('vaultCertName' in props) {
const cert = getSecretOutput({
name: props.vaultCertName,
vaultInfo: this.args.vaultInfo!,
});
return { encodedCertificate: cert.apply((c) => c!.value!) };
}

return {
encodedCertificate: props.certificate,
certificatePassword: props.certificatePassword,
};
}

private buildAPIM() {
const { group, envRoles } = this.args;
const { group, envRoles, vaultInfo } = this.args;

const sku = {
name: this._sku!.sku,
Expand All @@ -141,8 +167,8 @@ class ApimBuilder
this._apimInstance = new apim.ApiManagementService(
this._instanceName,
{
serviceName: this._instanceName,
...group,
serviceName: this._instanceName,
publisherEmail: this._publisher!.publisherEmail,
publisherName: this._publisher!.publisherName ?? organization,
notificationSenderEmail:
Expand All @@ -153,26 +179,29 @@ class ApimBuilder
sku,

certificates: [
...this._rootCerts.map((c) => ({
encodedCertificate: c.certificate,
certificatePassword: c.certificatePassword,
storeName: 'Root',
})),
...this._caCerts.map((c) => ({
encodedCertificate: c.certificate,
certificatePassword: c.certificatePassword,
storeName: 'CertificateAuthority',
})),
...this._rootCerts.map((c) => {
const crt = this.getCert(c);
return {
...crt,
storeName: 'Root',
};
}),
...this._caCerts.map((c) => {
const crt = this.getCert(c);
return {
...crt,
storeName: 'CertificateAuthority',
};
}),
],

enableClientCertificate: true,
hostnameConfigurations: this._proxyDomain
? [
{
...this.getCert(this._proxyDomain),
type: 'Proxy',
hostName: this._proxyDomain.domain,
encodedCertificate: this._proxyDomain.certificate,
certificatePassword: this._proxyDomain.certificatePassword,
negotiateClientCertificate: false,
defaultSslBinding: false,
},
Expand Down Expand Up @@ -257,6 +286,16 @@ class ApimBuilder
if (this._envRoleType && envRoles) {
envRoles.addIdentity(this._envRoleType, this._apimInstance.identity);
}

if (vaultInfo) {
addCustomSecret({
name: `${this._instanceName}-host`,
value: this._proxyDomain?.domain ?? this._apimInstance.gatewayUrl,
contentType: `APIM ${this._instanceName}`,
dependsOn: this._apimInstance,
vaultInfo,
});
}
}

private buildEntraID() {
Expand Down Expand Up @@ -346,7 +385,8 @@ class ApimBuilder
});
}
private buildInsightLog() {
if (!this.args.logInfo?.appInsight) return;
const { logInfo } = this.args;
if (!logInfo?.appInsight) return;
//App Insight Logs
new apim.Logger(
`${this._instanceName}-insight`,
Expand All @@ -356,11 +396,11 @@ class ApimBuilder

loggerType: apim.LoggerType.ApplicationInsights,
description: 'App Insight Logger',
loggerId: randomUuId(this._instanceName!).result,
resourceId: this.args.logInfo.appInsight.id,
loggerId: `${this._instanceName}-appInsight`,
resourceId: logInfo!.appInsight.id,
credentials: {
//This credential will be added to NameValue automatically.
instrumentationKey: this.args.logInfo?.appInsight.instrumentationKey!,
instrumentationKey: logInfo!.appInsight.instrumentationKey!,
},
},
{ dependsOn: this._apimInstance },
Expand Down
19 changes: 18 additions & 1 deletion src/Builder/ApimPolicyBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export default class ApimPolicyBuilder implements types.IApimPolicyBuilder {
return this;
}

public setBaseUrlIf(
condition: boolean,
props: types.ApimBaseUrlType,
): types.IApimPolicyBuilder {
if (condition) this.setBaseUrl(props);
return this;
}
public setHeader(props: types.ApimSetHeaderType): types.IApimPolicyBuilder {
let rs = `\t<set-header name="${props.name}" exists-action="${props.type}">`;
if (props.value) {
Expand Down Expand Up @@ -234,7 +241,7 @@ export default class ApimPolicyBuilder implements types.IApimPolicyBuilder {
return this;
}

public forwardToServiceBus(
public forwardToBus(
props: types.ApimForwardToServiceBusType,
): types.IApimPolicyBuilder {
this.authIdentity({
Expand All @@ -243,9 +250,11 @@ export default class ApimPolicyBuilder implements types.IApimPolicyBuilder {
resource: 'https://servicebus.azure.net',
ignoreError: false,
});

this.setBaseUrl({
url: `https://${props.serviceBusName}.servicebus.windows.net`,
});

this.rewriteUri({ template: `${props.topicOrQueueName}/messages` });
if (props.brokerProperties) {
Object.keys(props.brokerProperties).forEach((key) =>
Expand All @@ -259,6 +268,14 @@ export default class ApimPolicyBuilder implements types.IApimPolicyBuilder {
return this;
}

public forwardToBusIf(
condition: boolean,
props: types.ApimForwardToServiceBusType,
): types.IApimPolicyBuilder {
if (condition) this.forwardToBus(props);
return this;
}

public setResponseHeaders(
props: types.ApimSetHeaderType,
): types.IApimPolicyBuilder {
Expand Down
1 change: 1 addition & 0 deletions src/Builder/VnetBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class VnetBuilder
this._ipType = type;
return this;
}

public withPublicIPFrom(id: Input<string>): IGatewayFireWallBuilder {
this._finalIpAddressIds.push(id);
this._ipType = 'existing';
Expand Down
12 changes: 8 additions & 4 deletions src/Builder/types/apimBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
EnvRoleKeyTypes,
PrivateLinkPropsType,
ResourceInfo,
CertType,
VaultCertType,
WithEnvRoles,
WithLogInfo,
} from '../../types';
Expand Down Expand Up @@ -38,10 +40,8 @@ export type ApimPublisherBuilderType = {
/**
* Type for configuring certificates for APIM.
*/
export type ApimCertBuilderType = {
certificate: Input<string>;
certificatePassword?: Input<string>;
};

export type ApimCertBuilderType = CertType | VaultCertType;

/**
* Type for configuring domain and certificates for APIM.
Expand Down Expand Up @@ -174,6 +174,10 @@ export interface IApimBuilder extends IBuilder<ResourceInfo>, IApimAuthBuilder {
* @returns The APIM builder instance.
*/
withProxyDomain(props: ApimDomainBuilderType): IApimBuilder;
withProxyDomainIf(
condition: boolean,
props: ApimDomainBuilderType,
): IApimBuilder;

// withInsightLog(props: AppInsightInfo): IApimBuilder;

Expand Down
7 changes: 6 additions & 1 deletion src/Builder/types/apimPolicyBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export interface IApimPolicyBuilder {
* @returns The policy builder instance.
*/
setBaseUrl(props: ApimBaseUrlType): IApimPolicyBuilder;
setBaseUrlIf(condition: boolean, props: ApimBaseUrlType): IApimPolicyBuilder;

/**
* Sets a header.
Expand Down Expand Up @@ -242,7 +243,11 @@ export interface IApimPolicyBuilder {
*/
verifyClientCert(props: ApimClientCertType): IApimPolicyBuilder;

forwardToServiceBus(props: ApimForwardToServiceBusType): IApimPolicyBuilder;
forwardToBus(props: ApimForwardToServiceBusType): IApimPolicyBuilder;
forwardToBusIf(
condition: boolean,
props: ApimForwardToServiceBusType,
): IApimPolicyBuilder;

setResponseHeaders(props: ApimSetHeaderType): IApimPolicyBuilder;

Expand Down
2 changes: 1 addition & 1 deletion src/Builder/types/sqlBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type SqlBuilderAuthOptionsType = Omit<
/**
* Arguments for defining a SQL database.
*/
export type SqlDbBuilderType = {
export type SqlDbBuilderType = WithLockable & {
/**
* The name of the database.
*/
Expand Down
Loading

0 comments on commit 203296d

Please sign in to comment.