Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

feat: APIM feature updates #312

Merged
merged 12 commits into from
Sep 6, 2019
8 changes: 3 additions & 5 deletions src/plugins/identity/azureKeyVaultPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ export class AzureKeyVaultPlugin extends AzureBasePlugin {
if (!keyVaultConfig) {
return Promise.resolve();
}
this.serverless.cli.log("Starting KeyVault service setup");
this.log("Starting KeyVault service setup");

const keyVaultService = new AzureKeyVaultService(this.serverless, this.options);
const result = await keyVaultService.setPolicy(keyVaultConfig);
await keyVaultService.setPolicy(keyVaultConfig);

this.serverless.cli.log("Finished KeyVault service setup");

return result;
this.log("Finished KeyVault service setup");
}
}
1 change: 0 additions & 1 deletion src/services/apimService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ describe("APIM Service", () => {
responses: [],
}
);
fail();
});
});

Expand Down
15 changes: 7 additions & 8 deletions src/services/azureKeyVaultService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ export interface AzureKeyVaultConfig {
* Services for the Key Vault Plugin
*/
export class AzureKeyVaultService extends BaseService {
private funcApp: FunctionAppService;

/**
* Initialize key vault service and get function app
* @param serverless Serverless object
* @param options Serverless CLI options
*/
public constructor(serverless: Serverless, options: Serverless.Options) {
super(serverless, options);
this.funcApp = new FunctionAppService(serverless, options);
}

/**
Expand All @@ -36,13 +33,15 @@ export class AzureKeyVaultService extends BaseService {
*/
public async setPolicy(keyVaultConfig: AzureKeyVaultConfig) {
const subscriptionID = this.subscriptionId;
const functionAppService = new FunctionAppService(this.serverless, this.options);
const keyVaultClient = new KeyVaultManagementClient(this.credentials, subscriptionID);

const func = await this.funcApp.get();
const identity = func.identity;
const functionApp = await functionAppService.get();
const identity = functionApp.identity;
let vault: Vault;
const keyVaultClient = new KeyVaultManagementClient(this.credentials, subscriptionID);

try {
vault = await keyVaultClient.vaults.get(keyVaultConfig.resourceGroup, keyVaultConfig.name)
vault = await keyVaultClient.vaults.get(keyVaultConfig.resourceGroup, keyVaultConfig.name);
} catch (error) {
throw new Error("Error: Specified vault not found")
}
Expand All @@ -56,6 +55,6 @@ export class AzureKeyVaultService extends BaseService {
}
vault.properties.accessPolicies.push(newEntry);

return keyVaultClient.vaults.createOrUpdate(keyVaultConfig.resourceGroup, keyVaultConfig.name, {location: vault.location, properties: vault.properties})
await keyVaultClient.vaults.createOrUpdate(keyVaultConfig.resourceGroup, keyVaultConfig.name, {location: vault.location, properties: vault.properties});
}
}
6 changes: 2 additions & 4 deletions src/services/baseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ export abstract class BaseService {
this.storageAccountName = StorageAccountResource.getResourceName(this.config);

if (!this.credentials && authenticate) {
throw new Error(
`Azure Credentials has not been set in ${this.constructor.name}`
);
throw new Error(`Azure Credentials has not been set in ${this.constructor.name}`);
}
}

Expand Down Expand Up @@ -196,7 +194,7 @@ export abstract class BaseService {
* Log message to Serverless CLI
* @param message Message to log
*/
protected log(message: string, options?: ServerlessLogOptions, entity?: string,) {
protected log(message: string, options?: ServerlessLogOptions, entity?: string, ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Remove last comma and empty space from method header

(this.serverless.cli.log as any)(message, entity, options);
}

Expand Down
11 changes: 5 additions & 6 deletions src/services/loginService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class AzureLoginService extends BaseService {
* set or via interactive login if environment variables are not set
* @param options Options for different authentication methods
*/
public async login(options?: AzureTokenCredentialsOptions|InteractiveLoginOptions): Promise<AuthResponse> {
public async login(options?: AzureTokenCredentialsOptions | InteractiveLoginOptions): Promise<AuthResponse> {
const subscriptionId = process.env.azureSubId;
const clientId = process.env.azureServicePrincipalClientId;
const secret = process.env.azureServicePrincipalPassword;
Expand All @@ -40,22 +40,21 @@ export class AzureLoginService extends BaseService {
}

public async interactiveLogin(options?: InteractiveLoginOptions): Promise<AuthResponse> {
let authResp: AuthResponse = {credentials: undefined, subscriptions: []};
let authResp: AuthResponse = { credentials: undefined, subscriptions: [] };
const fileTokenCache = new SimpleFileTokenCache();
if(fileTokenCache.isEmpty()){
if (fileTokenCache.isEmpty()) {
await open("https://microsoft.com/devicelogin");
authResp = await interactiveLoginWithAuthResponse({...options, tokenCache: fileTokenCache});
authResp = await interactiveLoginWithAuthResponse({ ...options, tokenCache: fileTokenCache });
fileTokenCache.addSubs(authResp.subscriptions);
} else {
authResp.credentials = new DeviceTokenCredentials(undefined, undefined, fileTokenCache.first().userId, undefined, undefined, fileTokenCache);
authResp.subscriptions = fileTokenCache.listSubscriptions();
}

return authResp;

}

public async servicePrincipalLogin(clientId: string, secret: string, tenantId: string, options: AzureTokenCredentialsOptions): Promise<AuthResponse> {
return loginWithServicePrincipalSecretWithAuthResponse(clientId, secret, tenantId, options);
return await loginWithServicePrincipalSecretWithAuthResponse(clientId, secret, tenantId, options);
wbreza marked this conversation as resolved.
Show resolved Hide resolved
}
}
6 changes: 3 additions & 3 deletions src/services/packageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export class PackageService extends BaseService {
/**
* Creates the function.json binding files required for the serverless service
*/
public createBindings(): Promise<any[]> {
public async createBindings(): Promise<void> {
const createEventsPromises = this.serverless.service.getAllFunctions()
.map((functionName) => {
const metaData = Utils.getFunctionMetaData(functionName, this.serverless);
return this.createBinding(functionName, metaData);
});

return Promise.all(createEventsPromises);
await Promise.all(createEventsPromises);
}

/**
Expand Down Expand Up @@ -100,6 +100,7 @@ export class PackageService extends BaseService {
// Find incoming binding within functionJSON and set the route
const index = (functionJSON.bindings as any[])
.findIndex((binding) => (!binding.direction || binding.direction === "in"));

functionJSON.bindings[index].route = bindingAzureSettings.route;
}

Expand All @@ -109,7 +110,6 @@ export class PackageService extends BaseService {
}

const functionJsonString = JSON.stringify(functionJSON, null, 2);

fs.writeFileSync(path.join(functionDirPath, "function.json"), functionJsonString);

return Promise.resolve();
Expand Down