@@ -7,10 +7,15 @@ import { getEnv } from './env.js';
7
7
const serviceId = argv [ 2 ] ;
8
8
const serviceName = argv [ 3 ] ;
9
9
10
- const { DICTIONARY_NAME , CONFIG_STORE_NAME , KV_STORE_NAME , SECRET_STORE_NAME } =
11
- getEnv ( serviceName ) ;
10
+ const {
11
+ DICTIONARY_NAME ,
12
+ CONFIG_STORE_NAME ,
13
+ KV_STORE_NAME ,
14
+ SECRET_STORE_NAME ,
15
+ ACL_NAME ,
16
+ } = getEnv ( serviceName ) ;
12
17
13
- function existingStoreId ( stores , existingName ) {
18
+ function existingListId ( stores , existingName ) {
14
19
const existing = stores . find (
15
20
( { Name, name } ) => name === existingName || Name === existingName ,
16
21
) ;
@@ -34,108 +39,112 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
34
39
}
35
40
zx . verbose = true ;
36
41
}
37
- const FASTLY_API_TOKEN = process . env . FASTLY_API_TOKEN ;
38
42
39
43
async function setupConfigStores ( ) {
40
- let stores = await ( async function ( ) {
41
- try {
42
- return JSON . parse (
43
- await zx `fastly config-store list --quiet --json --token $FASTLY_API_TOKEN` ,
44
- ) ;
45
- } catch {
46
- return [ ] ;
47
- }
48
- } ) ( ) ;
44
+ const stores = JSON . parse (
45
+ await zx `fastly config-store list --quiet --json --token $FASTLY_API_TOKEN` ,
46
+ ) ;
49
47
50
- let STORE_ID = existingStoreId ( stores , DICTIONARY_NAME ) ;
48
+ let STORE_ID = existingListId ( stores , DICTIONARY_NAME ) ;
51
49
if ( ! STORE_ID ) {
52
50
console . log ( `Creating new config store ${ DICTIONARY_NAME } ` ) ;
53
- process . env . STORE_ID = JSON . parse (
51
+ STORE_ID = JSON . parse (
54
52
await zx `fastly config-store create --quiet --name=${ DICTIONARY_NAME } --json --token $FASTLY_API_TOKEN` ,
55
53
) . id ;
56
54
} else {
57
55
console . log ( `Using existing config store ${ DICTIONARY_NAME } ` ) ;
58
- process . env . STORE_ID = STORE_ID ;
56
+ STORE_ID = STORE_ID ;
59
57
}
60
- await zx `echo -n 'https://twitter.com/fastly' | fastly config-store-entry update --upsert --key twitter --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN` ;
58
+ await zx `echo -n 'https://twitter.com/fastly' | fastly config-store-entry update --upsert --key twitter --store-id=${ STORE_ID } --stdin --token $FASTLY_API_TOKEN` ;
61
59
try {
62
- await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone` ;
60
+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ STORE_ID } --token $FASTLY_API_TOKEN --autoclone` ;
63
61
} catch ( e ) {
64
62
if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
65
63
}
66
64
67
- STORE_ID = existingStoreId ( stores , CONFIG_STORE_NAME ) ;
65
+ STORE_ID = existingListId ( stores , CONFIG_STORE_NAME ) ;
68
66
if ( ! STORE_ID ) {
69
67
console . log ( `Creating new config store ${ CONFIG_STORE_NAME } ` ) ;
70
- process . env . STORE_ID = JSON . parse (
68
+ STORE_ID = JSON . parse (
71
69
await zx `fastly config-store create --quiet --name=${ CONFIG_STORE_NAME } --json --token $FASTLY_API_TOKEN` ,
72
70
) . id ;
73
71
} else {
74
72
console . log ( `Using existing config store ${ CONFIG_STORE_NAME } ` ) ;
75
- process . env . STORE_ID = STORE_ID ;
73
+ STORE_ID = STORE_ID ;
76
74
}
77
- await zx `echo -n 'https://twitter.com/fastly' | fastly config-store-entry update --upsert --key twitter --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN` ;
75
+ await zx `echo -n 'https://twitter.com/fastly' | fastly config-store-entry update --upsert --key twitter --store-id=${ STORE_ID } --stdin --token $FASTLY_API_TOKEN` ;
78
76
try {
79
- await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone` ;
77
+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ STORE_ID } --token $FASTLY_API_TOKEN --autoclone` ;
80
78
} catch ( e ) {
81
79
if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
82
80
}
83
81
}
84
82
85
83
async function setupKVStore ( ) {
86
- let stores = await ( async function ( ) {
87
- try {
88
- return JSON . parse (
89
- await zx `fastly kv-store list --quiet --json --token $FASTLY_API_TOKEN` ,
90
- ) . Data ;
91
- } catch {
92
- return [ ] ;
93
- }
94
- } ) ( ) ;
84
+ let stores = JSON . parse (
85
+ await zx `fastly kv-store list --quiet --json --token $FASTLY_API_TOKEN` ,
86
+ ) . Data ;
95
87
96
- const STORE_ID = existingStoreId ( stores , KV_STORE_NAME ) ;
88
+ let STORE_ID = existingListId ( stores , KV_STORE_NAME ) ;
97
89
if ( ! STORE_ID ) {
98
90
console . log ( `Creating new KV store ${ KV_STORE_NAME } ` ) ;
99
- process . env . STORE_ID = JSON . parse (
91
+ STORE_ID = JSON . parse (
100
92
await zx `fastly kv-store create --quiet --name=${ KV_STORE_NAME } --json --token $FASTLY_API_TOKEN` ,
101
93
) . StoreID ;
102
94
} else {
103
95
console . log ( `Using existing KV store ${ KV_STORE_NAME } ` ) ;
104
- process . env . STORE_ID = STORE_ID ;
96
+ STORE_ID = STORE_ID ;
105
97
}
106
98
try {
107
- await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone` ;
99
+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ STORE_ID } --token $FASTLY_API_TOKEN --autoclone` ;
108
100
} catch ( e ) {
109
101
if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
110
102
}
111
103
}
112
104
113
105
async function setupSecretStore ( ) {
114
- let stores = await ( async function ( ) {
115
- try {
116
- return JSON . parse (
117
- await zx `fastly secret-store list --quiet --json --token $FASTLY_API_TOKEN` ,
118
- ) ;
119
- } catch ( e ) {
120
- throw e ;
121
- }
122
- } ) ( ) ;
123
- const STORE_ID = existingStoreId ( stores , SECRET_STORE_NAME ) ;
106
+ const stores = JSON . parse (
107
+ await zx `fastly secret-store list --quiet --json --token $FASTLY_API_TOKEN` ,
108
+ ) ;
109
+ let STORE_ID = existingListId ( stores , SECRET_STORE_NAME ) ;
124
110
if ( ! STORE_ID ) {
125
111
console . log ( `Creating new secret store ${ SECRET_STORE_NAME } ` ) ;
126
- process . env . STORE_ID = JSON . parse (
112
+ STORE_ID = JSON . parse (
127
113
await zx `fastly secret-store create --quiet --name=${ SECRET_STORE_NAME } --json --token $FASTLY_API_TOKEN` ,
128
114
) . id ;
129
115
} else {
130
116
console . log ( `Using existing secret store ${ SECRET_STORE_NAME } ` ) ;
131
- process . env . STORE_ID = STORE_ID ;
132
117
}
133
- await zx `echo -n 'This is also some secret data' | fastly secret-store-entry create --recreate-allow --name first --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN` ;
118
+ await zx `echo -n 'This is also some secret data' | fastly secret-store-entry create --recreate-allow --name first --store-id=${ STORE_ID } --stdin --token $FASTLY_API_TOKEN` ;
134
119
let key =
135
120
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ;
136
- await zx `echo -n 'This is some secret data' | fastly secret-store-entry create --recreate-allow --name ${ key } --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN` ;
121
+ await zx `echo -n 'This is some secret data' | fastly secret-store-entry create --recreate-allow --name ${ key } --store-id=${ STORE_ID } --stdin --token $FASTLY_API_TOKEN` ;
122
+ try {
123
+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ STORE_ID } --token $FASTLY_API_TOKEN --autoclone` ;
124
+ } catch ( e ) {
125
+ if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
126
+ }
127
+ }
128
+
129
+ async function setupAcl ( ) {
130
+ let ACL_ID = existingListId (
131
+ JSON . parse (
132
+ await zx `fastly compute acl list-acls --quiet --json --token $FASTLY_API_TOKEN` ,
133
+ ) . data ,
134
+ ACL_NAME ,
135
+ ) ;
136
+ if ( ! ACL_ID ) {
137
+ console . log ( `Creating ACL ${ ACL_NAME } ` ) ;
138
+ ACL_ID = JSON . parse (
139
+ await zx `fastly compute acl create --name=${ ACL_NAME } --token $FASTLY_API_TOKEN --json` ,
140
+ ) . id ;
141
+ await zx `fastly compute acl update --acl-id=${ ACL_ID } --operation=create --prefix=100.100.0.0/16 --action=BLOCK --token $FASTLY_API_TOKEN` ;
142
+ await zx `fastly compute acl update --acl-id=${ ACL_ID } --operation=create --prefix=2a03:4b80::/32 --action=ALLOW --token $FASTLY_API_TOKEN` ;
143
+ } else {
144
+ console . log ( `Using existing ACL ${ ACL_NAME } ` ) ;
145
+ }
137
146
try {
138
- await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone` ;
147
+ await zx `fastly resource-link create --service-id ${ serviceId } --version latest --resource-id ${ ACL_ID } --token $FASTLY_API_TOKEN --autoclone` ;
139
148
} catch ( e ) {
140
149
if ( ! e . message . includes ( 'Duplicate record' ) ) throw e ;
141
150
}
@@ -144,5 +153,6 @@ async function setupSecretStore() {
144
153
await setupConfigStores ( ) ;
145
154
await setupKVStore ( ) ;
146
155
await setupSecretStore ( ) ;
156
+ await setupAcl ( ) ;
147
157
148
158
await zx `fastly service-version activate --service-id ${ serviceId } --version latest --token $FASTLY_API_TOKEN` ;
0 commit comments