Skip to content

Commit

Permalink
update API and README
Browse files Browse the repository at this point in the history
  • Loading branch information
markussiebert committed Apr 10, 2022
1 parent 35435f3 commit 3bd5794
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 15 deletions.
7 changes: 6 additions & 1 deletion .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ project.prettier.addIgnorePattern('API.md');
project.prettier.addIgnorePattern('package.json');
project.jest.addIgnorePattern('/lambda/');
project.gitignore.addPatterns('/assets');
project.npmignore.addPatterns('/lambda', '/dist-lambda', '/scripts', '!/assets');
project.npmignore.addPatterns(
'/lambda',
'/dist-lambda',
'/scripts',
'!/assets',
);

goreleaserArtifactsNamespace = 'build-artifact-goreleaser';

Expand Down
68 changes: 68 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ const sopsSecretProps: SopsSecretProps = { ... }
| <code><a href="#cdk-sops-secrets.SopsSecretProps.property.sopsKmsKey">sopsKmsKey</a></code> | <code>aws-cdk-lib.aws_kms.IKey[]</code> | The kmsKey used to encrypt the sops file. |
| <code><a href="#cdk-sops-secrets.SopsSecretProps.property.sopsProvider">sopsProvider</a></code> | <code><a href="#cdk-sops-secrets.SopsSyncProvider">SopsSyncProvider</a></code> | The custom resource provider to use. |
| <code><a href="#cdk-sops-secrets.SopsSecretProps.property.stringifyValues">stringifyValues</a></code> | <code>boolean</code> | Shall all values be flattened? |
| <code><a href="#cdk-sops-secrets.SopsSecretProps.property.uploadType">uploadType</a></code> | <code><a href="#cdk-sops-secrets.UploadType">UploadType</a></code> | How should the secret be passed to the CustomResource? |

---

Expand Down Expand Up @@ -1397,6 +1398,19 @@ are lookup errors for certain float types

---

##### `uploadType`<sup>Optional</sup> <a name="uploadType" id="cdk-sops-secrets.SopsSecretProps.property.uploadType"></a>

```typescript
public readonly uploadType: UploadType;
```

- *Type:* <a href="#cdk-sops-secrets.UploadType">UploadType</a>
- *Default:* INLINE

How should the secret be passed to the CustomResource?

---

### SopsSyncOptions <a name="SopsSyncOptions" id="cdk-sops-secrets.SopsSyncOptions"></a>

Configuration options for the SopsSync.
Expand All @@ -1421,6 +1435,7 @@ const sopsSyncOptions: SopsSyncOptions = { ... }
| <code><a href="#cdk-sops-secrets.SopsSyncOptions.property.sopsKmsKey">sopsKmsKey</a></code> | <code>aws-cdk-lib.aws_kms.IKey[]</code> | The kmsKey used to encrypt the sops file. |
| <code><a href="#cdk-sops-secrets.SopsSyncOptions.property.sopsProvider">sopsProvider</a></code> | <code><a href="#cdk-sops-secrets.SopsSyncProvider">SopsSyncProvider</a></code> | The custom resource provider to use. |
| <code><a href="#cdk-sops-secrets.SopsSyncOptions.property.stringifyValues">stringifyValues</a></code> | <code>boolean</code> | Shall all values be flattened? |
| <code><a href="#cdk-sops-secrets.SopsSyncOptions.property.uploadType">uploadType</a></code> | <code><a href="#cdk-sops-secrets.UploadType">UploadType</a></code> | How should the secret be passed to the CustomResource? |

---

Expand Down Expand Up @@ -1540,6 +1555,19 @@ are lookup errors for certain float types

---

##### `uploadType`<sup>Optional</sup> <a name="uploadType" id="cdk-sops-secrets.SopsSyncOptions.property.uploadType"></a>

```typescript
public readonly uploadType: UploadType;
```

- *Type:* <a href="#cdk-sops-secrets.UploadType">UploadType</a>
- *Default:* INLINE

How should the secret be passed to the CustomResource?

---

### SopsSyncProps <a name="SopsSyncProps" id="cdk-sops-secrets.SopsSyncProps"></a>

The configuration options extended by the target Secret.
Expand All @@ -1564,6 +1592,7 @@ const sopsSyncProps: SopsSyncProps = { ... }
| <code><a href="#cdk-sops-secrets.SopsSyncProps.property.sopsKmsKey">sopsKmsKey</a></code> | <code>aws-cdk-lib.aws_kms.IKey[]</code> | The kmsKey used to encrypt the sops file. |
| <code><a href="#cdk-sops-secrets.SopsSyncProps.property.sopsProvider">sopsProvider</a></code> | <code><a href="#cdk-sops-secrets.SopsSyncProvider">SopsSyncProvider</a></code> | The custom resource provider to use. |
| <code><a href="#cdk-sops-secrets.SopsSyncProps.property.stringifyValues">stringifyValues</a></code> | <code>boolean</code> | Shall all values be flattened? |
| <code><a href="#cdk-sops-secrets.SopsSyncProps.property.uploadType">uploadType</a></code> | <code><a href="#cdk-sops-secrets.UploadType">UploadType</a></code> | How should the secret be passed to the CustomResource? |
| <code><a href="#cdk-sops-secrets.SopsSyncProps.property.secret">secret</a></code> | <code>aws-cdk-lib.aws_secretsmanager.ISecret</code> | The secret that will be populated with the encrypted sops file content. |

---
Expand Down Expand Up @@ -1684,6 +1713,19 @@ are lookup errors for certain float types

---

##### `uploadType`<sup>Optional</sup> <a name="uploadType" id="cdk-sops-secrets.SopsSyncProps.property.uploadType"></a>

```typescript
public readonly uploadType: UploadType;
```

- *Type:* <a href="#cdk-sops-secrets.UploadType">UploadType</a>
- *Default:* INLINE

How should the secret be passed to the CustomResource?

---

##### `secret`<sup>Required</sup> <a name="secret" id="cdk-sops-secrets.SopsSyncProps.property.secret"></a>

```typescript
Expand All @@ -1698,3 +1740,29 @@ The secret that will be populated with the encrypted sops file content.



## Enums <a name="Enums" id="Enums"></a>

### UploadType <a name="UploadType" id="cdk-sops-secrets.UploadType"></a>

#### Members <a name="Members" id="Members"></a>

| **Name** | **Description** |
| --- | --- |
| <code><a href="#cdk-sops-secrets.UploadType.INLINE">INLINE</a></code> | Pass the secret data inline (base64 encoded and compressed). |
| <code><a href="#cdk-sops-secrets.UploadType.ASSET">ASSET</a></code> | Uplaod the secert data as asset. |

---

##### `INLINE` <a name="INLINE" id="cdk-sops-secrets.UploadType.INLINE"></a>

Pass the secret data inline (base64 encoded and compressed).

---


##### `ASSET` <a name="ASSET" id="cdk-sops-secrets.UploadType.ASSET"></a>

Uplaod the secert data as asset.

---

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ Sometimes it can be necessary to access the IAM role of the SopsSync provider. I
});
```

### UploadType: INLINE / ASSET — What when why?

I decided, that the default behavior should be "INLINE" because of the following consideration:

* Fewer permissions: If we use inline content instead of a S3 asset, the SopsSyncProvider does not need permissions to access the asset bucket and its KMS key.
* Faster: If we don't have to upload and download things from and to S3, it should be a little faster.
* Interchangeable: As we use the same information to generate the version of the secret, no new version of the secret should be created, if you change from INLINE to ASSET or vice versa, even if the CloudFormation resource updates.
* I personally think sops files are not that big, that we should run into limits, but if so — we can change to asset ```uploadType```.

You can change the uplaodType via the properties:

```typescript
const secret = new SopsSecret(this, 'SopsWithAssetUpload', {
sopsFilePath: 'secrets/sopsfile-encrypted.json',
uploadType: UploadType.ASSET // instead of the default UploadType.INLINE
});
```
## Motivation

I have created this project to solve a recurring problem of syncing Mozilla/sops secrets into AWS SecretsManager in a convenient, secure way.
Expand Down
30 changes: 16 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface SopsSyncOptions {
* How should the secret be passed to the CustomResource?
* @default INLINE
*/
readonly uploadType?: UploadType
readonly uploadType?: UploadType;

/**
* The format of the sops file.
Expand Down Expand Up @@ -180,29 +180,27 @@ export class SopsSync extends Construct {
throw new Error(`File ${props.sopsFilePath} does not exist!`);
}



/**
* Handle uploadType INLINE or ASSET
*/
const uploadType = props.uploadType ?? UploadType.INLINE;
let sopsAsset:Asset|undefined = undefined;
let sopsInline:{Content:string, Hash:string}|undefined = undefined;
let sopsS3File:{Bucket:string, Key:string}|undefined = undefined;
if ( uploadType === UploadType.INLINE ) {
let sopsAsset: Asset | undefined = undefined;
let sopsInline: { Content: string; Hash: string } | undefined = undefined;
let sopsS3File: { Bucket: string; Key: string } | undefined = undefined;
if (uploadType === UploadType.INLINE) {
sopsInline = {
Content: fs.readFileSync(props.sopsFilePath).toString('base64'),
// We calculate the hash the same way as it would be done by new Asset(..) - so we can ensure stable version names even if switching from INLINE to ASSET and viceversa.
Hash: FileSystem.fingerprint(props.sopsFilePath),
}
} else if ( uploadType === UploadType.ASSET ) {
};
} else if (uploadType === UploadType.ASSET) {
sopsAsset = new Asset(this, 'Asset', {
path: props.sopsFilePath,
});
sopsS3File = {
sopsS3File = {
Bucket: sopsAsset.bucket.bucketName,
Key: sopsAsset.s3ObjectKey,
}
};
} else {
throw new Error(`Unsupported UploadType: ${uploadType}`);
}
Expand All @@ -222,12 +220,16 @@ export class SopsSync extends Construct {
);
}
props.secret.grantWrite(provider);
if ( sopsAsset !== undefined ) {
if (sopsAsset !== undefined) {
sopsAsset.bucket.grantRead(provider);
}
} else {
Annotations.of(this).addWarning(
`Please ensure propper permissions for the passed lambda function:\n - write Access to the secret\n - encrypt with the sopsKmsKey${uploadType === UploadType.ASSET ? '\n - download from asset bucket' : ''}`,
`Please ensure propper permissions for the passed lambda function:\n - write Access to the secret\n - encrypt with the sopsKmsKey${
uploadType === UploadType.ASSET
? '\n - download from asset bucket'
: ''
}`,
);
}
if (props.sopsAgeKey !== undefined) {
Expand All @@ -240,7 +242,7 @@ export class SopsSync extends Construct {
properties: {
SecretARN: props.secret.secretArn,
SopsS3File: sopsS3File,
SopsInline: sopsInline,
SopsInline: sopsInline,
ConvertToJSON: this.converToJSON,
Flatten: this.flatten,
Format: this.sopsFileFormat,
Expand Down

0 comments on commit 3bd5794

Please sign in to comment.