Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloud9): support setting environment owner #23878

Merged
merged 8 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion packages/@aws-cdk/aws-cloud9/lib/environment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as ec2 from '@aws-cdk/aws-ec2';
import { IUser } from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnEnvironmentEC2 } from '../lib/cloud9.generated';
Expand Down Expand Up @@ -53,11 +54,17 @@ export enum ImageId {
*/
UBUNTU_18_04 = 'ubuntu-18.04-x86_64'
}

/**
* Properties for Ec2Environment
*/
export interface Ec2EnvironmentProps {
/**
* The type of owner environment.
*
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
* @default - string
*/
readonly owner?: Owner;

/**
* The type of instance to connect to the environment.
*
Expand Down Expand Up @@ -137,6 +144,13 @@ export class Ec2Environment extends cdk.Resource implements IEc2Environment {
return new Import(scope, id);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't remove this line, we try to keep an empty line between functions so they are easier to visually distinguish.

/**
* The Environment Owner of the ownerarn
*
* @attribute
*/
public readonly owner?: Owner;

rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
/**
* The environment name of this Cloud9 environment
*
Expand Down Expand Up @@ -170,6 +184,7 @@ export class Ec2Environment extends cdk.Resource implements IEc2Environment {
super(scope, id);

this.vpc = props.vpc;
this.owner = props.owner;
if (!props.subnetSelection && this.vpc.publicSubnets.length === 0) {
throw new Error('no subnetSelection specified and no public subnet found in the vpc, please specify subnetSelection');
}
Expand All @@ -182,6 +197,7 @@ export class Ec2Environment extends cdk.Resource implements IEc2Environment {
const c9env = new CfnEnvironmentEC2(this, 'Resource', {
name: props.ec2EnvironmentName,
description: props.description,
ownerArn: props.owner?.ownerArn,
instanceType: props.instanceType?.toString() ?? ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO).toString(),
subnetId: this.vpc.selectSubnets(vpcSubnets).subnetIds[0],
repositories: props.clonedRepositories ? props.clonedRepositories.map(r => ({
Expand Down Expand Up @@ -217,3 +233,34 @@ export class CloneRepository {

private constructor(public readonly repositoryUrl: string, public readonly pathComponent: string) {}
}

/**
* The class for different types of owners
*/
export class Owner {
/**
* import from Owner Iuser
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* import from Owner Iuser
* Make an IAM user the environment owner

*
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
* @param user environment owner can be an IAM user.
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
*/
public static user(user: IUser): Owner {
return { ownerArn: user.userArn };
}

/**
* import from Owner account root
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* import from Owner account root
* Make the Account Root User the environment owner (not recommended)

*
* @param accountId environment owner can be a root account.
*/
public static accountRoot(accountId: string): Owner {
return { ownerArn: `arn:aws:iam::${accountId}:root` };
}

/**
* import owenrArn
*
* @param ownerArn of environment owner.
*/
private constructor(public readonly ownerArn: string) {}
}

43 changes: 42 additions & 1 deletion packages/@aws-cdk/aws-cloud9/test/cloud9.environment.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Match, Template } from '@aws-cdk/assertions';
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as cloud9 from '../lib';
import { ConnectionType, ImageId } from '../lib';
import { ConnectionType, ImageId, Owner } from '../lib';

let stack: cdk.Stack;
let vpc: ec2.IVpc;
Expand Down Expand Up @@ -79,9 +80,49 @@ test('throw error when subnetSelection not specified and the provided VPC has no
test('can use CodeCommit repositories', () => {
// WHEN
const repo = codecommit.Repository.fromRepositoryName(stack, 'Repo', 'foo');
const user = new iam.User(stack, 'User');
new cloud9.Ec2Environment(stack, 'C9Env', {
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
vpc,
clonedRepositories: [
cloud9.CloneRepository.fromCodeCommit(repo, '/src'),
],
imageId: cloud9.ImageId.AMAZON_LINUX_2,
owner: Owner.user(user),
});
// THEN

Template.fromStack(stack).hasResourceProperties('AWS::Cloud9::EnvironmentEC2', {
InstanceType: 't2.micro',
Repositories: [
{
PathComponent: '/src',
RepositoryUrl: {
'Fn::Join': [
'',
[
'https://git-codecommit.',
{
Ref: 'AWS::Region',
},
'.',
{
Ref: 'AWS::URLSuffix',
},
'/v1/repos/foo',
],
],
},
},
],
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
});
});

test('can use CodeCommit repo', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please describe what the test is trying to assert here

// WHEN
const repo = codecommit.Repository.fromRepositoryName(stack, 'Repo', 'foo');
new cloud9.Ec2Environment(stack, 'C9Env', {
vpc,
owner: Owner.accountRoot('12345678'),
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
clonedRepositories: [
cloud9.CloneRepository.fromCodeCommit(repo, '/src'),
],
Expand Down