|
| 1 | +import { |
| 2 | + Annotations, |
| 3 | + IResource, |
| 4 | + RemovalPolicy, |
| 5 | + Resource, |
| 6 | + Tag, |
| 7 | +} from 'aws-cdk-lib'; |
| 8 | +import { CfnConnection } from 'aws-cdk-lib/aws-codestarconnections'; |
| 9 | + |
| 10 | +import { Construct } from 'constructs'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Connection Provider Type supported |
| 14 | + */ |
| 15 | +export enum CodeStarConnectionProviderType { |
| 16 | + /** |
| 17 | + * Github provider |
| 18 | + */ |
| 19 | + GITHUB = 'GitHub', |
| 20 | + |
| 21 | + /** |
| 22 | + * Github Entrprise provider |
| 23 | + */ |
| 24 | + GITHUB_ENTERPRISE = 'GitHubEnterpriseServer', |
| 25 | + |
| 26 | + /** |
| 27 | + * Gitlab Provider |
| 28 | + */ |
| 29 | + GITLAB = 'GitLab', |
| 30 | + |
| 31 | + /** |
| 32 | + * Bitbucket Provider |
| 33 | + */ |
| 34 | + BITBUCKET = 'Bitbucket', |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Actions defined by AWS CodeStar Connections |
| 39 | + * Service prefix: codestar-connections |
| 40 | + * |
| 41 | + * The Resource Type indicates whether each action supports resource-level permissions. |
| 42 | + * If there is no value, you must specify all resources ("*") to which the policy applies |
| 43 | + * in the Resource element of your policy statement |
| 44 | + * |
| 45 | + * The Condition Keys includes keys that you can specify in a policy statement's Condition element |
| 46 | + */ |
| 47 | +export enum CodeStarConnectionPolicyActions { |
| 48 | + /** |
| 49 | + * Grants permission to create a Connection resource |
| 50 | + * @accessLevel Write |
| 51 | + * @conditionKeys aws:RequestTag/${aws:TagKeys} |
| 52 | + * @conditionKeys aws:TagKeys |
| 53 | + * @conditionKeys codestar-connections:ProviderType |
| 54 | + */ |
| 55 | + CREATE_CONNECTION = 'codestar-connections:CreateConnection', |
| 56 | + |
| 57 | + /** |
| 58 | + * Grants permission to create a Host resource |
| 59 | + * @accessLevel Write |
| 60 | + * @conditionKeys aws:RequestTag/${aws:TagKeys} |
| 61 | + * @conditionKeys aws:TagKeys |
| 62 | + * @conditionKeys codestar-connections:ProviderType |
| 63 | + */ |
| 64 | + CREATE_HOST = 'codestar-connections:CreateHost', |
| 65 | + |
| 66 | + /** |
| 67 | + * Grants permission to delete a Connection resource |
| 68 | + * @accessLevel Write |
| 69 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:connection/${ConnectionId} (Required) |
| 70 | + */ |
| 71 | + DELETE_CONNECTION = 'codestar-connections:DeleteConnection', |
| 72 | + |
| 73 | + /** |
| 74 | + * Grants permission to delete a host resource |
| 75 | + * @accessLevel Write |
| 76 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:host/${HostId} (Required) |
| 77 | + */ |
| 78 | + DELETE_HOST = 'codestar-connections:DeleteHost', |
| 79 | + |
| 80 | + /** |
| 81 | + * Grants permission to get details about a Connection resource |
| 82 | + * @accessLevel Read |
| 83 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:connection/${ConnectionId} (Required) |
| 84 | + */ |
| 85 | + GET_CONNECTION = 'codestar-connections:GetConnection', |
| 86 | + |
| 87 | + /** |
| 88 | + * Grants permission to get details about a host resource |
| 89 | + * @accessLevel Read |
| 90 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:host/${HostId} (Required) |
| 91 | + */ |
| 92 | + GET_HOST = 'codestar-connections:GetHost', |
| 93 | + |
| 94 | + /** |
| 95 | + * Grants permission to associate a third party, such as a Bitbucket App installation, with a Connection |
| 96 | + * @accessLevel Read |
| 97 | + * @conditionKeys codestar-connections:ProviderType |
| 98 | + * @dependsOn codestar-connections:StarOAuthHandshake |
| 99 | + */ |
| 100 | + GET_INDIVIDUAL_ACCESS_TOKEN = 'codestar-connections:GetIndividualAccessToken', |
| 101 | + |
| 102 | + /** |
| 103 | + * Grants permission to associate a third party, such as a Bitbucket App installation, with a Connection |
| 104 | + * @accessLevel Read |
| 105 | + * @conditionKeys codestar-connections:ProviderType |
| 106 | + * @dependsOn codestar-connections:StarOAuthHandshake |
| 107 | + */ |
| 108 | + GET_INSTALLATION_URL_FOR_APP = 'codestar-connections:GetInstallationUrlForApp', |
| 109 | + |
| 110 | + /** |
| 111 | + * Grants permission to get the URL to authorize an installation in a third party app |
| 112 | + * @accessLevel Read |
| 113 | + * @conditionKeys codestar-connections:ProviderType |
| 114 | + * @dependsOn codestar-connections:StarOAuthHandshake |
| 115 | + */ |
| 116 | + GET_INSTALLATION_URL_FOR_HOST = 'codestar-connections:GetInstallationUrlForHost', |
| 117 | + |
| 118 | + /** |
| 119 | + * Grants permission to get the URL to authorize an installation in a third party app |
| 120 | + * @accessLevel Read |
| 121 | + * @conditionKeys codestar-connections:ProviderType |
| 122 | + */ |
| 123 | + GET_INSTALLATION_URL = 'codestar-connections:GetInstallationUrl', |
| 124 | + |
| 125 | + /** |
| 126 | + * Grants permission to list Connection resources |
| 127 | + * @accessLevel List |
| 128 | + * @conditionKeys codestar-connections:ProviderTypeFilter |
| 129 | + */ |
| 130 | + LIST_CONNECTIONS = 'codestar-connections:ListConnections', |
| 131 | + |
| 132 | + /** |
| 133 | + * Grants permission to list host resources |
| 134 | + * @accessLevel List |
| 135 | + * @conditionKeys codestar-connections:ProviderTypeFilter |
| 136 | + */ |
| 137 | + LIST_HOSTS = 'codestar-connections:ListHosts', |
| 138 | + |
| 139 | + /** |
| 140 | + * Grants permission to list individual access token |
| 141 | + * @accessLevel List |
| 142 | + */ |
| 143 | + LIST_INDIVIDUAL_ACCESS_TOKENS = 'codestar-connections:ListIndividualAccessTokens', |
| 144 | + |
| 145 | + /** |
| 146 | + * Grants permission to list installations |
| 147 | + * @accessLevel List |
| 148 | + */ |
| 149 | + LIST_INSTALLATIONS = 'codestar-connections:ListInstallations', |
| 150 | + |
| 151 | + /** |
| 152 | + * Grants permission to associate a third party, such as a Bitbucket App installation, with a Connection |
| 153 | + * @accessLevel List |
| 154 | + * @dependson codestar-connections:GetIndividualAccessToken |
| 155 | + * @dependsOn codestar-connections:StarOAuthHandshake |
| 156 | + */ |
| 157 | + LIST_INSTALLATION_TARGETS = 'codestar-connections:ListInstallationTargets', |
| 158 | + |
| 159 | + /** |
| 160 | + * Grants permission to list pass connections |
| 161 | + * @accessLevel List |
| 162 | + */ |
| 163 | + LIST_PASS_CONNECTIONS = 'codestar-connections:ListPassConnections', |
| 164 | + |
| 165 | + /** |
| 166 | + * Grants permission to the set of key-value pairs that are used to manage the resource |
| 167 | + * @accessList List |
| 168 | + * @resouceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:connection/${ConnectionId} (Required) |
| 169 | + */ |
| 170 | + LIST_TAGS_FOR_RESOURCE = 'codestar-connections:ListTagsForResource', |
| 171 | + |
| 172 | + /** |
| 173 | + * Grants permission to pass a Connection resource to an AWS service that accepts a Connection ARN as input, such as codepipeline:CreatePipeline |
| 174 | + * @accessLevel Read |
| 175 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:connection/${ConnectionId} |
| 176 | + * @conditionKeys codestar-connections:PassToService |
| 177 | + */ |
| 178 | + PASS_CONNECTION = 'codestar-connections:PassConnection', |
| 179 | + |
| 180 | + /** |
| 181 | + * Grants permission to associate a third party server, such as a GitHub Enterprise Server instance, with a Host |
| 182 | + * @accessLevel Read |
| 183 | + * @conditionKeys codestar-connections:HostArn |
| 184 | + */ |
| 185 | + REGISTER_APP_CODE = 'codestar-connections:RegisterAppCode', |
| 186 | + |
| 187 | + /** |
| 188 | + * Grants permission to associate a third party server, such as a GitHub Enterprise Server instance, with a Host |
| 189 | + * @accessLevel Read |
| 190 | + * @conditionKeys codestar-connections:HostArn |
| 191 | + */ |
| 192 | + START_APP_REGISTRATION_HANDSHAKE = 'codestar-connections:StartAppRegistrationHandshake', |
| 193 | + |
| 194 | + /** |
| 195 | + * Grants permission to associate a third party, such as a Bitbucket App installation, with a Connection |
| 196 | + * @accessLevel Read |
| 197 | + * @conditionKeys codestar-connections:ProviderType |
| 198 | + */ |
| 199 | + START_OAUTH_HANDSHAKE = 'codestar-connections:StartOAuthHandshake', |
| 200 | + |
| 201 | + /** |
| 202 | + * Grants permission to add or modify the tags of the given resource |
| 203 | + * @accessLevel Tagging |
| 204 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:connection/${ConnectionId} (Required) |
| 205 | + * @conditionKeys aws:RequestTag/${aws:TagKeys} |
| 206 | + * @conditionKeys aws:TagKeys |
| 207 | + */ |
| 208 | + TAG_RESOURCE = 'codestar-connections:TagResource', |
| 209 | + |
| 210 | + /** |
| 211 | + * Grants permission to remove tags from an AWS resource |
| 212 | + * @accessLevel Tagging |
| 213 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:connection/${ConnectionId} (Required) |
| 214 | + * @conditionKeys aws:TagKeys |
| 215 | + */ |
| 216 | + UNTAG_RESOURCE = 'codestar-connections:UntagResource', |
| 217 | + |
| 218 | + /** |
| 219 | + * Grants permission to update a Connection resource with an installation of the CodeStar Connections App |
| 220 | + * @accessLevel Write |
| 221 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:connection/${ConnectionId} (Required) |
| 222 | + * @dependsOn codestar-connections:GetInstallationUrl |
| 223 | + * @dependsOn codestar-connections:GetIndividualAccessToken |
| 224 | + * @dependsOn codestar-connections:StarOAuthHandshake |
| 225 | + * @dependsOn codestar-connections:ListInstallationTargets |
| 226 | + * @conditionKeys codestar-connections:InstallationId |
| 227 | + */ |
| 228 | + UPDATE_CONNECTION_INSTALLATION = 'codestar-connections:UpdateConnectionInstallation', |
| 229 | + |
| 230 | + /** |
| 231 | + * Grants permission to update a host resource |
| 232 | + * @accessLevel Write |
| 233 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:host/${HostId} (Required) |
| 234 | + */ |
| 235 | + UPDATE_HOST = 'codestar-connections:UpdateHost', |
| 236 | + |
| 237 | + /** |
| 238 | + * Grants permission to use a Connection resource to call provider actions |
| 239 | + * @accessLevel Read |
| 240 | + * @resourceTypes arn:${Partition}:codestar-connections:${Region}:${Account}:connection/${ConnectionId} (Required) |
| 241 | + * @conditionKeys codestar-connections:FullRepositoryId |
| 242 | + * @conditionKeys codestar-connections:ProviderAction |
| 243 | + * @conditionKeys codestar-connections:ProviderPermissionsRequired |
| 244 | + */ |
| 245 | + USE_CONNECTION = 'codestar-connections:UseConnection', |
| 246 | +} |
| 247 | + |
| 248 | +export interface ICodeStarConnection extends IResource { |
| 249 | + /** |
| 250 | + * The name of the connection. |
| 251 | + * Connection names must be in an AWS user account. |
| 252 | + */ |
| 253 | + readonly connectionName: string; |
| 254 | + |
| 255 | + /** |
| 256 | + * The ARN of the connection. |
| 257 | + */ |
| 258 | + readonly connectionArn: string; |
| 259 | +} |
| 260 | + |
| 261 | +export abstract class CodeStarConnectionBase |
| 262 | + extends Resource |
| 263 | + implements ICodeStarConnection |
| 264 | +{ |
| 265 | + /** |
| 266 | + * The name of the Code Star connection |
| 267 | + */ |
| 268 | + public abstract readonly connectionName: string; |
| 269 | + |
| 270 | + /** |
| 271 | + * The ARN of the Code Star connection |
| 272 | + */ |
| 273 | + public abstract readonly connectionArn: string; |
| 274 | +} |
| 275 | + |
| 276 | +export interface CodeStarConnectionProps { |
| 277 | + /** |
| 278 | + * The name of the connection. |
| 279 | + * Connection names must be in an AWS user account. |
| 280 | + */ |
| 281 | + readonly connectionName: string; |
| 282 | + |
| 283 | + /** |
| 284 | + * The type of the connection. |
| 285 | + */ |
| 286 | + readonly providerType: CodeStarConnectionProviderType; |
| 287 | + |
| 288 | + /** |
| 289 | + * The Amazon Resource Name (ARN) of the host associated with the connection. |
| 290 | + */ |
| 291 | + readonly hostArn?: string; |
| 292 | + |
| 293 | + /** |
| 294 | + * The list of tags associated with the connection. |
| 295 | + */ |
| 296 | + readonly tags?: Tag[]; |
| 297 | + |
| 298 | + /** |
| 299 | + * Determine what happens to the repository when the resource/stack is deleted. |
| 300 | + * |
| 301 | + * @default RemovalPolicy.Retain |
| 302 | + */ |
| 303 | + readonly removalPolicy?: RemovalPolicy; |
| 304 | +} |
| 305 | + |
| 306 | +/** |
| 307 | + * Define a CodeStar Connection resource |
| 308 | + * @resource AWS::CodeStarConnections::Connection |
| 309 | + * @example |
| 310 | + * |
| 311 | + * new CodeStarConnection(this, 'MyConnection', { |
| 312 | + * connectionName: 'MyConnection', |
| 313 | + * providerType: 'GitHub', |
| 314 | + * tags: [{ |
| 315 | + * key: 'key', |
| 316 | + * value: 'value', |
| 317 | + * }], |
| 318 | + * } |
| 319 | + */ |
| 320 | +export class CodeStarConnection extends CodeStarConnectionBase { |
| 321 | + public readonly connectionName: string; |
| 322 | + public readonly connectionArn: string; |
| 323 | + |
| 324 | + constructor(scope: Construct, id: string, props: CodeStarConnectionProps) { |
| 325 | + super(scope, id, { |
| 326 | + physicalName: props.connectionName, |
| 327 | + }); |
| 328 | + |
| 329 | + this.validateConnectionName(props.connectionName); |
| 330 | + |
| 331 | + const resource = new CfnConnection(this, 'Resource', { |
| 332 | + connectionName: props.connectionName, |
| 333 | + providerType: props.providerType, |
| 334 | + hostArn: props.hostArn, |
| 335 | + tags: props.tags, |
| 336 | + }); |
| 337 | + |
| 338 | + resource.applyRemovalPolicy(props.removalPolicy); |
| 339 | + |
| 340 | + this.connectionName = props.connectionName; |
| 341 | + this.connectionArn = resource.attrConnectionArn; |
| 342 | + } |
| 343 | + |
| 344 | + private validateConnectionName(qualifier: string): void { |
| 345 | + // Rules codified from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codestarconnections-connection.html#cfn-codestarconnections-connection-connectionname |
| 346 | + if (qualifier.length < 2 || qualifier.length > 32) { |
| 347 | + Annotations.of(this).addError( |
| 348 | + 'Connection Name must be at least 1 and no more than 32 characters' |
| 349 | + ); |
| 350 | + } |
| 351 | + } |
| 352 | +} |
0 commit comments