Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
fix(bridge): Remove line breaks and unnecessary escaping in strings i…
Browse files Browse the repository at this point in the history
…n webhook.yaml (#7025)

* fix(bridge): Remove line breaks in strings in webhook.yaml

Signed-off-by: Klaus Strießnig <k.striessnig@gmail.com>

* added unit tests

Signed-off-by: Klaus Strießnig <k.striessnig@gmail.com>

* set curl to block folded

Signed-off-by: Klaus Strießnig <k.striessnig@gmail.com>

* fix types

Signed-off-by: Klaus Strießnig <k.striessnig@gmail.com>

* type safety for yaml order

Signed-off-by: Klaus Strießnig <k.striessnig@gmail.com>

* move tests to correct file

Signed-off-by: Klaus Strießnig <k.striessnig@gmail.com>
  • Loading branch information
Kirdock committed Mar 11, 2022
1 parent 9b69d64 commit 23ac339
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 9 deletions.
145 changes: 145 additions & 0 deletions bridge/server/models/webhook-config-yaml.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,82 @@ import { WebhookConfigYaml } from './webhook-config-yaml';
import { WebhookConfig } from '../../shared/models/webhook-config';
import { Webhook } from '../interfaces/webhook-config-yaml-result';

const config = WebhookConfigYaml.fromJSON({
kind: 'WebhookConfig',
apiVersion: 'webhookconfig.keptn.sh/v1alpha1',
spec: {
webhooks: [
{
subscriptionID: 'myID',
type: 'sh.keptn.event.deployment.started',
requests: [
`curl http://keptn.sh/asdf asdf --request GET --proxy http://keptn.sh/proxy --data '{"data": "myData"}'`,
],
envFrom: [
{
name: 'mySecret',
secretRef: {
name: 'myName',
key: 'myKey',
},
},
],
sendFinished: true,
},
],
},
metadata: {
name: 'webhook-configuration',
},
});
const configWithLongCurl = WebhookConfigYaml.fromJSON({
kind: 'WebhookConfig',
apiVersion: 'webhookconfig.keptn.sh/v1alpha1',
spec: {
webhooks: [
{
subscriptionID: 'myID',
type: 'sh.keptn.event.deployment.started',
requests: [
`curl http://keptn.sh/asdf asdf --request GET --header 'content-type: application/json' --header 'Authorization: myVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongHeader' --proxy http://keptn.sh/proxy --data '{"data": "myData"}'`,
],
envFrom: [
{
name: 'mySecret',
secretRef: {
name: 'myName',
key: 'myKey',
},
},
],
sendFinished: true,
},
],
},
metadata: {
name: 'webhook-configuration',
},
});
const configWithoutSecrets = WebhookConfigYaml.fromJSON({
kind: 'WebhookConfig',
apiVersion: 'webhookconfig.keptn.sh/v1alpha1',
spec: {
webhooks: [
{
subscriptionID: 'myID',
type: 'sh.keptn.event.deployment.started',
requests: [
`curl http://keptn.sh/asdf asdf --request GET --header 'content-type: application/json' --header 'Authorization: myVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongHeader' --proxy http://keptn.sh/proxy --data '{"data": "myData"}'`,
],
sendFinished: true,
},
],
},
metadata: {
name: 'webhook-configuration',
},
});

describe('Test webhook-config-yaml', () => {
it('should parse request correctly', () => {
// given
Expand Down Expand Up @@ -164,6 +240,75 @@ describe('Test webhook-config-yaml', () => {
expect(yamlConfig.spec.webhooks.length).toBe(0);
});

it('should generate yaml correctly', () => {
// when
const result = config.toYAML();
// then
expect(result).toBe(`apiVersion: webhookconfig.keptn.sh/v1alpha1
kind: WebhookConfig
metadata:
name: webhook-configuration
spec:
webhooks:
- subscriptionID: myID
type: sh.keptn.event.deployment.started
requests:
- >-
curl http://keptn.sh/asdf asdf --request GET --proxy http://keptn.sh/proxy --data '{"data": "myData"}'
envFrom:
- name: mySecret
secretRef:
name: myName
key: myKey
sendFinished: true
`);
});

it('should generate yaml correctly with long curl without line breaks or escape characters', () => {
// when
const result = configWithLongCurl.toYAML();

// then
expect(result).toBe(`apiVersion: webhookconfig.keptn.sh/v1alpha1
kind: WebhookConfig
metadata:
name: webhook-configuration
spec:
webhooks:
- subscriptionID: myID
type: sh.keptn.event.deployment.started
requests:
- >-
curl http://keptn.sh/asdf asdf --request GET --header 'content-type: application/json' --header 'Authorization: myVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongHeader' --proxy http://keptn.sh/proxy --data '{"data": "myData"}'
envFrom:
- name: mySecret
secretRef:
name: myName
key: myKey
sendFinished: true
`);
});

it('should generate yaml without secrets', () => {
// when
const result = configWithoutSecrets.toYAML();

// then
expect(result).toBe(`apiVersion: webhookconfig.keptn.sh/v1alpha1
kind: WebhookConfig
metadata:
name: webhook-configuration
spec:
webhooks:
- subscriptionID: myID
type: sh.keptn.event.deployment.started
requests:
- >-
curl http://keptn.sh/asdf asdf --request GET --header 'content-type: application/json' --header 'Authorization: myVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongHeader' --proxy http://keptn.sh/proxy --data '{"data": "myData"}'
sendFinished: true
`);
});

function getDefaultWebhookYaml(sendFinished?: boolean): WebhookConfigYaml {
return WebhookConfigYaml.fromJSON({
kind: 'WebhookConfig',
Expand Down
24 changes: 20 additions & 4 deletions bridge/server/models/webhook-config-yaml.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Yaml from 'yaml';
import { Document, Scalar, YAMLMap, YAMLSeq } from 'yaml';
import { WebhookConfigMethod } from '../../shared/interfaces/webhook-config';
import { WebhookConfig, WebhookSecret } from '../../shared/models/webhook-config';
import { Webhook, WebhookConfigYamlResult } from '../interfaces/webhook-config-yaml-result';
import { parseCurl } from '../utils/curl.utils';

const order: { [key: string]: number } = {
const order: { [key in keyof WebhookConfigYamlResult]: number } = {
apiVersion: 0,
kind: 1,
metadata: 2,
Expand Down Expand Up @@ -148,8 +148,24 @@ export class WebhookConfigYaml implements WebhookConfigYamlResult {
}

public toYAML(): string {
return Yaml.stringify(this, {
sortMapEntries: (a, b) => order[a.key] - order[b.key],
const yamlDoc = new Document(this, {
sortMapEntries: (a, b): number =>
order[a.key as keyof WebhookConfigYamlResult] - order[b.key as keyof WebhookConfigYamlResult],
toStringDefaults: {
lineWidth: 0,
},
});
this.setCurlToBlockFolded(yamlDoc);
return yamlDoc.toString();
}

private setCurlToBlockFolded(yamlDoc: Document): void {
const yamlSeq = yamlDoc.getIn(['spec', 'webhooks'], true) as YAMLSeq;
for (const webhookYaml of yamlSeq.items) {
const requests = (webhookYaml as YAMLMap).get('requests', true) as YAMLSeq;
for (const curl of requests.items) {
(curl as Scalar).type = 'BLOCK_FOLDED';
}
}
}
}
2 changes: 1 addition & 1 deletion bridge/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"openid-client": "5.1.4",
"pug": "3.0.2",
"semver": "7.3.5",
"yaml": "1.10.2"
"yaml": "2.0.0-10"
},
"devDependencies": {
"@types/adm-zip": "0.4.34",
Expand Down
8 changes: 4 additions & 4 deletions bridge/server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4515,10 +4515,10 @@ yallist@^4.0.0:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==

yaml@1.10.2:
version "1.10.2"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==
yaml@2.0.0-10:
version "2.0.0-10"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.0.0-10.tgz#d5b59e2d14b8683313a534f2bbc648e211a2753e"
integrity sha512-FHV8s5ODFFQXX/enJEU2EkanNl1UDBUz8oa4k5Qo/sR+Iq7VmhCDkRMb0/mjJCNeAWQ31W8WV6PYStDE4d9EIw==

yargs-parser@20.x, yargs-parser@^20.2.2:
version "20.2.9"
Expand Down

0 comments on commit 23ac339

Please sign in to comment.