Skip to content

Commit 5375a25

Browse files
committed
GCP: add support to firewall
1 parent fa10bc2 commit 5375a25

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

deploys/consul/default.nix

+9-10
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ let
3737
datacenter = elemAt name_match 0;
3838
replica = elemAt name_match 1;
3939
};
40+
filterHostsBy = datacenter:
41+
map (nodeName: nodes.${nodeName}.config.deployment.targetHost)
42+
(filter (a: (strings.hasPrefix datacenter a)) (attrNames nodes));
43+
primary_hosts = filterHostsBy primary_datacenter;
44+
hosts = filterHostsBy datacenter;
45+
isPrimary = primary_datacenter == datacenter;
4046
in {
4147
imports = [ ./gateway.nix ./templates.nix ];
4248
networking.hostName = name;
43-
networking.extraHosts = concatMapStrings (hostName: ''
44-
${config.deployment.targetHost} ${hostName}
49+
networking.extraHosts = concatMapStrings (name: ''
50+
${nodes.${name}.config.deployment.targetHost} ${name}
4551
'') (attrNames nodes);
4652

4753
# use xlbs to build envoyPackage
@@ -86,14 +92,7 @@ in {
8692
enable = true;
8793
extraConfigFiles = [ "/etc/consul.d/encryption.hcl" ];
8894
leaveOnStop = true;
89-
extraConfig = let
90-
filterHostsBy = datacenter:
91-
map (nodeName: nodes.${nodeName}.config.deployment.targetHost)
92-
(filter (a: (strings.hasPrefix datacenter a)) (attrNames nodes));
93-
primary_hosts = filterHostsBy primary_datacenter;
94-
hosts = filterHostsBy datacenter;
95-
isPrimary = primary_datacenter == datacenter;
96-
in {
95+
extraConfig = {
9796
inherit domain datacenter primary_datacenter;
9897
ui_config = { enabled = true; };
9998
server = true;

env/gcp/config.nix

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
};
3131
};
3232

33+
firewall = {
34+
test-allow-all-to-consul = {
35+
description = "allow all consul ips to tagged consul machines";
36+
source_tags = [ "consul" "test" ];
37+
target_tags = [ "consul" "test" ];
38+
network = "test";
39+
allow = [{
40+
protocol = "tcp";
41+
ports = [ "0-65535" ];
42+
}];
43+
};
44+
};
45+
3346
replicas = {
3447
c1r1 = {
3548
tags = [ "consul" "server" "nixos" "test" ];

provision/gcp/default.nix

+35-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ in {
5757
};
5858
});
5959

60+
firewallModule = submodule ({ config, name, ... }: {
61+
options = {
62+
project = mk' str gcp.project "project";
63+
location = mk' str gcp.region "location";
64+
source_tags = mk' (listOf str) [ name ] "tags";
65+
target_tags = mk' (listOf str) [ name ] "tags";
66+
description = mk' str "description ${name}" "rule description";
67+
network = mk' str "default" "network interface used";
68+
allow = mk' (listOf rulesModule) [ ] "allowed rules";
69+
deny = mk' (listOf rulesModule) [ ] "denied rules";
70+
};
71+
});
72+
73+
rulesModule = submodule {
74+
options = {
75+
protocol = mk' str "all" "protocol";
76+
ports = mk' (listOf str) [ ] "ports allowed";
77+
};
78+
};
79+
6080
replicasModule = submodule ({ config, name, ... }: {
6181
options = {
6282
project = mk' str gcp.project "project";
@@ -100,6 +120,13 @@ in {
100120
description = "image options";
101121
};
102122

123+
# rules submodule
124+
firewall = mkOption {
125+
type = (attrsOf firewallModule);
126+
default = { };
127+
description = "rules options";
128+
};
129+
103130
# replica submodule
104131
replicas = mkOption {
105132
type = (attrsOf replicasModule);
@@ -117,7 +144,7 @@ in {
117144
networks = gcp.networks;
118145
images = gcp.images;
119146
replicas = gcp.replicas;
120-
147+
firewall = gcp.firewall;
121148
in {
122149
terraform.required_providers =
123150
mkIf gcp.enable { google.source = "hashicorp/google"; };
@@ -199,6 +226,13 @@ in {
199226
};
200227
});
201228

229+
google_compute_firewall = attrsMap firewall (name: {
230+
${name} = with firewall.${name}; {
231+
inherit name project description network source_tags target_tags allow
232+
deny;
233+
};
234+
});
235+
202236
};
203237
output = attrsMap replicas (name:
204238
let

0 commit comments

Comments
 (0)