Skip to content

Commit

Permalink
interim example of l2 renaming instead of remapping attrs for iam
Browse files Browse the repository at this point in the history
  • Loading branch information
moofish32 committed May 14, 2019
1 parent 3ff28ad commit 4808108
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-iam/lib/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Group extends Resource implements IIdentity {
});

this.groupName = group.groupName;
this.groupArn = group.groupArn;
this.groupArn = group.attrArn;
this.policyFragment = new ArnPrincipal(this.groupArn).policyFragment;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-iam/lib/lazy-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ export class LazyRole extends cdk.Construct implements IRole {
/**
* Returns the ARN of this role.
*/
public get roleArn(): string {
return this.instantiate().roleArn;
public get attrArn(): string {
return this.instantiate().attrArn;
}

public get roleId(): string {
return this.instantiate().roleId;
public get attrRoleId(): string {
return this.instantiate().attrRoleId;
}

public get roleName(): string {
Expand Down
32 changes: 16 additions & 16 deletions packages/@aws-cdk/aws-iam/lib/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ export class Role extends Resource implements IRole {
/**
* Returns the ARN of this role.
*/
public readonly roleArn: string;
public readonly attrArn: string;

/**
* Returns the stable and unique string identifying the role. For example,
* AIDAJQABLZS4A3QDU576Q.
*/
public readonly roleId: string;
public readonly attrRoleId: string;

/**
* Returns the name of the role.
Expand Down Expand Up @@ -152,10 +152,10 @@ export class Role extends Resource implements IRole {
maxSessionDuration: props.maxSessionDurationSec,
});

this.roleId = role.roleId;
this.roleArn = role.roleArn;
this.attrRoleId = role.attrRoleId;
this.attrArn = role.attrArn;
this.roleName = role.roleName;
this.policyFragment = new ArnPrincipal(this.roleArn).policyFragment;
this.policyFragment = new ArnPrincipal(this.attrArn).policyFragment;

function _flatten(policies?: { [name: string]: PolicyDocument }) {
if (policies == null || Object.keys(policies).length === 0) {
Expand All @@ -172,8 +172,8 @@ export class Role extends Resource implements IRole {

public export(): RoleImportProps {
return {
roleArn: new CfnOutput(this, 'RoleArn', { value: this.roleArn }).makeImportValue(),
roleId: new CfnOutput(this, 'RoleId', { value: this.roleId }).makeImportValue()
roleArn: new CfnOutput(this, 'RoleArn', { value: this.attrArn }).makeImportValue(),
roleId: new CfnOutput(this, 'RoleId', { value: this.attrRoleId }).makeImportValue()
};
}

Expand Down Expand Up @@ -215,7 +215,7 @@ export class Role extends Resource implements IRole {
return Grant.addToPrincipal({
grantee,
actions,
resourceArns: [this.roleArn],
resourceArns: [this.attrArn],
scope: this
});
}
Expand All @@ -235,13 +235,13 @@ export interface IRole extends IIdentity {
/**
* Returns the ARN of this role.
*/
readonly roleArn: string;
readonly attrArn: string;

/**
* Returns the stable and unique string identifying the role. For example,
* AIDAJQABLZS4A3QDU576Q.
*/
readonly roleId: string;
readonly attrRoleId: string;

/**
* Returns the name of this role.
Expand Down Expand Up @@ -313,26 +313,26 @@ class ImportedRole extends Construct implements IRole {
public readonly grantPrincipal: IPrincipal = this;
public readonly assumeRoleAction: string = 'sts:AssumeRole';
public readonly policyFragment: PrincipalPolicyFragment;
public readonly roleArn: string;
public readonly attrArn: string;

private readonly _roleId?: string;

constructor(scope: Construct, id: string, private readonly props: RoleImportProps) {
super(scope, id);
this.roleArn = props.roleArn;
this.attrArn = props.roleArn;
this._roleId = props.roleId;
this.policyFragment = new ArnPrincipal(this.roleArn).policyFragment;
this.policyFragment = new ArnPrincipal(this.attrArn).policyFragment;
}

public get roleId() {
public get attrRoleId() {
if (!this._roleId) {
throw new Error(`No roleId specified for imported role`);
}
return this._roleId;
}

public get roleName() {
return this.node.stack.parseArn(this.roleArn).resourceName!;
return this.node.stack.parseArn(this.attrArn).resourceName!;
}

public export() {
Expand All @@ -359,7 +359,7 @@ class ImportedRole extends Construct implements IRole {
return Grant.addToPrincipal({
grantee,
actions,
resourceArns: [this.roleArn],
resourceArns: [this.attrArn],
scope: this
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-iam/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class User extends Resource implements IIdentity {
});

this.userName = user.userName;
this.userArn = user.userArn;
this.userArn = user.attrArn;
this.policyFragment = new ArnPrincipal(this.userArn).policyFragment;

if (props.groups) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-iam/test/test.lazy-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export = nodeunit.testCase({
// WHEN
const roleArn = new iam.LazyRole(stack, 'Lazy', {
assumedBy: new iam.ServicePrincipal('test.service')
}).roleArn;
}).attrArn;

// THEN
test.notEqual(roleArn, null);
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-iam/test/test.role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ export = {
roleId: { 'Fn::ImportValue': 'Stack:MyRoleRoleIdF7B258D8' }
});

test.deepEqual(stack.node.resolve(importedRole.roleArn), { 'Fn::ImportValue': 'Stack:MyRoleRoleArn3388B7E2' });
test.deepEqual(stack.node.resolve(importedRole.roleId), { 'Fn::ImportValue': 'Stack:MyRoleRoleIdF7B258D8' });
test.deepEqual(stack.node.resolve(importedRole.attrArn), { 'Fn::ImportValue': 'Stack:MyRoleRoleArn3388B7E2' });
test.deepEqual(stack.node.resolve(importedRole.attrRoleId), { 'Fn::ImportValue': 'Stack:MyRoleRoleIdF7B258D8' });
test.deepEqual(stack.node.resolve(importedRole.roleName), {
'Fn::Select': [ 1, {
'Fn::Split': [ '/', {
Expand Down
6 changes: 5 additions & 1 deletion tools/awslint/lib/rules/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ resourceLinter.add({
const resourceAttributes = new Array<reflect.Property>();
for (const attr of e.ctx.resource.attributes) {
const attribute: reflect.Property | undefined = e.ctx.resourceInterface.ownProperties.find(p => p.name === attr);
// tslint:disable-next-line:no-console
e.ctx.resourceInterface.ownProperties.map(p => console.log(`name: ${p.name}, attr: ${attr}`));
const scope: string = e.ctx.resourceInterface.fqn + '.' + attr;
// tslint:disable-next-line:no-console
console.log(`${scope} :: ${attribute}`);
if (e.assert(attribute, scope)) {
resourceAttributes.push(attribute);
}
Expand Down Expand Up @@ -192,4 +196,4 @@ resourceLinter.add({
});
}
}
});
});
14 changes: 0 additions & 14 deletions tools/cfn2ts/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,7 @@ export default class CodeGenerator {
attributes.push(refAttr);
}

// //
// // Ref attribute
// //
// if (spec.RefKind !== schema.SpecialRefKind.None) {
// const refAttribute = genspec.refAttributeDefinition(resourceName, spec.RefKind!);
//
// // If there's already an attribute with the same name, ref is not needed
// if (!attributes.some(a => a.propertyName === refAttribute.propertyName)) {
// this.code.line(`public readonly ${refAttribute.propertyName}Attr: ${refAttribute.attributeType};`);
// attributes.push(refAttribute);
// }
// }

// set class properties to match CloudFormation Properties spec

if (propsType) {
this.emitPropsTypeProperties(resourceName, spec.Properties!, Container.Class);
}
Expand Down
5 changes: 3 additions & 2 deletions tools/cfn2ts/lib/genspec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export function validatorName(typeName: CodeName): CodeName {
*/
export function attributeDefinition(attributeName: string, spec: schema.Attribute): Attribute {
const descriptiveName = attributeName.replace(/\./g, '');
const propertyName = `${cloudFormationToScriptName(descriptiveName)}Attr`; // "bucketArn"
const suffixName = codemaker.toPascalCase(cloudFormationToScriptName(descriptiveName));
const propertyName = `attr${suffixName}`; // "attrArn"

let attrType: string;
if ('PrimitiveType' in spec && spec.PrimitiveType === 'String') {
Expand All @@ -193,7 +194,7 @@ export function refAttributeDefinition(): Attribute {

const constructorArguments = 'this.ref';

return new Attribute('refAttr', 'string', constructorArguments);
return new Attribute('attrRef', 'string', constructorArguments);
}

/**
Expand Down

0 comments on commit 4808108

Please sign in to comment.