-
Notifications
You must be signed in to change notification settings - Fork 14
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: Amazon Elastic Container Registry plugin #304
Draft
blakeromano
wants to merge
4
commits into
awslabs:main
Choose a base branch
from
blakeromano:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# ECR AWS plugin for Backstage | ||
|
||
This plugin is meant to allow you to view ECR Scan Results for a specific entity within your Backstage UI. | ||
|
||
This requires that where you run Backstage has AWS Credentials that has IAM Permissions to describe images and get ECR scan findings (through enviornment variables, IRSA, etc;). | ||
|
||
The plugin consists of the following packages: | ||
|
||
- `frontend`: The frontend plugin package installed in Backstage | ||
- `backend`: The backend plugin package installed in Backstage | ||
- `common`: Types and utilities shared between the packages | ||
|
||
## Installing | ||
|
||
This guide assumes that you are familiar with the general [Getting Started](../../docs/getting-started.md) documentation and have assumes you have an existing Backstage application. | ||
|
||
### Permissions | ||
|
||
The IAM role(s) used by Backstage will require the following permissions: | ||
|
||
```json | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"ecr:DescribeImages", | ||
"ecr:DescribeImageScanFindings" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Note: This policy does not reflect least privilege and you should further limit the policy to the appropriate AWS resources. | ||
|
||
### Backend package | ||
|
||
Install the backend package in your Backstage app: | ||
|
||
```shell | ||
yarn workspace backend add @aws/ecr-plugin-for-backstage-backend | ||
``` | ||
|
||
Add the plugin to the `packages/backend/src/index.ts`: | ||
|
||
```typescript | ||
const backend = createBackend(); | ||
// ... | ||
backend.add(import('@aws/ecr-plugin-for-backstage-backend')); | ||
// ... | ||
backend.start(); | ||
``` | ||
|
||
### Frontend package | ||
|
||
Install the frontend packages in your Backstage app: | ||
|
||
```shell | ||
yarn workspace app add @aws/ecr-plugin-for-backstage | ||
``` | ||
Edit the `packages/app/src/components/catalog/EntityPage.tsx` and add the imports | ||
|
||
```typescript jsx | ||
import { | ||
EntityEcrScanResultsContent, | ||
isAwsEcrScanResultsAvailable | ||
} from 'plugin-aws-ecr-scan'; | ||
``` | ||
|
||
Then add the following components: | ||
|
||
```typescript jsx | ||
<EntityLayout.Route path="/ecr-scan" title="Image Scan" if={isAwsEcrScanResultsAvailable}> | ||
<EntityEcrScanResultsContent /> | ||
</EntityLayout.Route> | ||
``` | ||
|
||
### Configuration | ||
blakeromano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Cost Insights requires minimal configuration in order to run. Add this to `app-config.yaml`: | ||
|
||
```yaml | ||
costInsights: | ||
engineerCost: 200000 | ||
``` | ||
|
||
## Entity annotations | ||
|
||
The plugin uses entity annotations to determine what queries to make for a given entity. The `aws.amazon.com/aws-ecr-repository-name` annotation can be added to any catalog entity to attach an ECR Repository to the entity. | ||
|
||
It is the users responsibility to ensure all AWS resources are tagged appropriately and that cost allocation tags have been enabled appropriately. | ||
blakeromano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```yaml | ||
apiVersion: backstage.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
# ... | ||
annotations: | ||
aws.amazon.com/aws-ecr-repository-name: my-service | ||
spec: | ||
type: service | ||
# ... | ||
``` | ||
|
||
## Configuration | ||
|
||
There are configuration options available to control the behavior of the plugin. | ||
|
||
```yaml | ||
aws: | ||
ecr: | ||
accountId: '1111111111' # (Optional) Use the specified AWS account ID | ||
region: 'us-west-2' # (Optional) Use the specified AWS region | ||
cache: | ||
enable: true # (Optional) Control is caching is enabled, defaults to true | ||
defaultTtl: 1000 # (Optional) How long responses are cached for in milliseconds, defaults to 1 day | ||
readTime: 1000 # (Optional) Read timeout when operating with cache in milliseconds, defaults to 1000 ms | ||
``` | ||
|
||
### Caching | ||
|
||
By default the responses from the backend plugin are cached for 1 day in order to improve performance to the user for querying the ECR API. The cache TTL can be configured to a different value or alternatively it can be disabled entirely if needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ECR AWS plugin for Backstage - Backend | ||
|
||
This a backend plugin package for Backstage related to ECR for AWS. For more information see the [documentation](../README.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createBackend } from '@backstage/backend-defaults'; | ||
|
||
const backend = createBackend(); | ||
backend.add(import('@backstage/plugin-auth-backend')); | ||
backend.add(import('@backstage/plugin-auth-backend-module-guest-provider')); | ||
backend.add(import('../src')); | ||
|
||
backend.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{ | ||
"name": "@aws/ecr-plugin-for-backstage-backend", | ||
"version": "0.1.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "github:awslabs/backstage-plugins-for-aws", | ||
"directory": "plugins/ecs/backend" | ||
}, | ||
"main": "src/index.ts", | ||
"types": "src/index.ts", | ||
"license": "Apache-2.0", | ||
"publishConfig": { | ||
"access": "public", | ||
"main": "dist/index.cjs.js", | ||
"types": "dist/index.d.ts" | ||
}, | ||
"backstage": { | ||
"role": "backend-plugin", | ||
"pluginId": "ecr-aws", | ||
"pluginPackages": [ | ||
"@aws/ecr-plugin-for-backstage", | ||
"@aws/ecr-plugin-for-backstage-backend" | ||
] | ||
}, | ||
"scripts": { | ||
"start": "backstage-cli package start", | ||
"build": "backstage-cli package build", | ||
"lint": "backstage-cli package lint", | ||
"test": "backstage-cli package test", | ||
"clean": "backstage-cli package clean", | ||
"prepack": "backstage-cli package prepack", | ||
"postpack": "backstage-cli package postpack" | ||
}, | ||
"dependencies": { | ||
"@aws-sdk/client-ecr": "^3.568.0", | ||
"@aws-sdk/middleware-sdk-sts": "^3.523.0", | ||
"@aws-sdk/types": "^3.609.0", | ||
"@aws-sdk/util-arn-parser": "^3.495.0", | ||
"@aws/aws-core-plugin-for-backstage-common": "workspace:^", | ||
"@backstage/backend-common": "^0.25.0", | ||
"@backstage/backend-plugin-api": "^1.0.2", | ||
"@backstage/catalog-client": "^1.8.0", | ||
"@backstage/catalog-model": "^1.7.1", | ||
"@backstage/config": "^1.3.0", | ||
"@backstage/errors": "^1.2.5", | ||
"@backstage/integration-aws-node": "^0.1.13", | ||
"@backstage/plugin-catalog-node": "^1.14.0", | ||
"@types/express": "*", | ||
"express": "^4.17.1", | ||
"express-promise-router": "^4.1.0", | ||
"luxon": "^3.4.4", | ||
"node-fetch": "^2.6.7", | ||
"regression": "^2.0.1", | ||
"winston": "^3.2.1", | ||
"yn": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"@backstage/backend-defaults": "^0.5.3", | ||
"@backstage/backend-test-utils": "^1.1.0", | ||
"@backstage/cli": "^0.29.2", | ||
"@backstage/plugin-auth-backend": "^0.24.0", | ||
"@backstage/plugin-auth-backend-module-guest-provider": "^0.2.2", | ||
"@types/luxon": "^3", | ||
"@types/regression": "^2", | ||
"@types/supertest": "^2.0.12", | ||
"aws-sdk-client-mock": "^4.0.0", | ||
"msw": "^1.0.0", | ||
"supertest": "^6.2.4" | ||
}, | ||
"files": [ | ||
"dist" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { assertError } from '@backstage/errors'; | ||
import { CacheService, LoggerService } from '@backstage/backend-plugin-api'; | ||
import { EcrConfig } from '../config'; | ||
|
||
const KEY_PREFIX = 'cost-insights:'; | ||
blakeromano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export class EcrCache { | ||
protected readonly cache: CacheService; | ||
protected readonly logger: LoggerService; | ||
protected readonly readTimeout: number; | ||
|
||
private constructor({ | ||
cache, | ||
logger, | ||
readTimeout, | ||
}: { | ||
cache: CacheService; | ||
logger: LoggerService; | ||
readTimeout: number; | ||
}) { | ||
this.cache = cache; | ||
this.logger = logger; | ||
this.readTimeout = readTimeout; | ||
} | ||
|
||
static fromConfig( | ||
config: EcrConfig, | ||
{ cache, logger }: { cache: CacheService; logger: LoggerService }, | ||
) { | ||
return new EcrCache({ | ||
cache: cache.withOptions({ defaultTtl: config.cache.defaultTtl }), | ||
logger, | ||
readTimeout: config.cache.readTimeout, | ||
}); | ||
} | ||
|
||
async get(path: string): Promise<string | undefined> { | ||
try { | ||
const response = (await Promise.race([ | ||
this.cache.get(`${KEY_PREFIX}${path}`), | ||
new Promise(cancelAfter => setTimeout(cancelAfter, this.readTimeout)), | ||
])) as string | undefined; | ||
|
||
if (response !== undefined) { | ||
this.logger.debug(`Cache hit: ${path}`); | ||
return response; | ||
} | ||
|
||
this.logger.debug(`Cache miss: ${path}`); | ||
return response; | ||
} catch (e) { | ||
assertError(e); | ||
this.logger.warn(`Error getting cache entry ${path}: ${e.message}`); | ||
this.logger.debug(e.stack || '-'); | ||
return undefined; | ||
} | ||
} | ||
|
||
async set(path: string, data: string): Promise<void> { | ||
this.logger.debug(`Writing cache entry for ${path}`); | ||
this.cache | ||
.set(`${KEY_PREFIX}${path}`, data) | ||
.catch(e => this.logger.error('write error', e)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './ECRCache'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmmm, I want this to work with this repo and the example CDK without any modification as all the other examples do.
Think we have two options:
aws-ecr-repository-name
, which doesn't have an equivalent in the other plugins