77using System . IO ;
88using System . Management . Automation ;
99using SIM . Loggers ;
10+ using YamlDotNet . RepresentationModel ;
11+ using System . Collections . Generic ;
12+ using System . Text . RegularExpressions ;
13+ using System . Linq ;
14+ using YamlDotNet . Serialization ;
1015
1116namespace SIM . Pipelines . Install . Containers
1217{
@@ -35,6 +40,8 @@ private ILogger Logger
3540 }
3641 }
3742
43+ private const string PathToCerts = @"C:\etc\traefik\certs" ;
44+
3845 private const string PathToCertFolder = "traefik\\ certs" ;
3946
4047 private const string PathToDynamicConfigFolder = "traefik\\ config\\ dynamic" ;
@@ -55,24 +62,29 @@ protected override void Process([NotNull] ProcessorArgs arguments)
5562
5663 Assert . ArgumentNotNullOrEmpty ( destinationFolder , "destinationFolder" ) ;
5764
58- UpdateTlsDynamicConfig ( args ) ;
65+ UpdateCertsConfigFile ( args ) ;
5966
60- string script = GetScript ( args ) ;
67+ string script = GetScript ( args . EnvModel ) ;
6168
6269 PSExecutor ps = new PSScriptExecutor ( destinationFolder , script ) ;
6370
6471 ExecuteScript ( ( ) => ps . Execute ( ) ) ;
6572 }
6673
67- private void UpdateTlsDynamicConfig ( InstallContainerArgs args )
74+ private void UpdateCertsConfigFile ( InstallContainerArgs args )
6875 {
69- string yamlContent = GetConfig ( args ) ;
76+ YamlDocument yamlDocument = GenerateCertsConfigFile ( args . EnvModel ) ;
7077
71- string yamlFileName = Path . Combine ( args . Destination , PathToDynamicConfigFolder , CertsConfigFileName ) ;
78+ string yamlFilePath = Path . Combine ( args . Destination , PathToDynamicConfigFolder , CertsConfigFileName ) ;
7279
7380 try
7481 {
75- UpdateConfigFile ( yamlFileName , yamlContent ) ;
82+ Serializer serializer = new Serializer ( ) ;
83+ using ( FileStream fileStream = File . OpenWrite ( yamlFilePath ) )
84+ using ( StreamWriter streamWriter = new StreamWriter ( fileStream ) )
85+ {
86+ serializer . Serialize ( streamWriter , yamlDocument . RootNode ) ;
87+ }
7688 }
7789 catch ( Exception ex )
7890 {
@@ -82,91 +94,52 @@ private void UpdateTlsDynamicConfig(InstallContainerArgs args)
8294 }
8395 }
8496
85- private string GetConfig ( InstallContainerArgs args )
97+ private List < string > GetHostnames ( EnvModel envModel )
8698 {
87- Topology topology = args . Topology ;
99+ Regex regex = new Regex ( DockerSettings . HostNameKeyPattern ) ;
100+
101+ string [ ] keys = envModel . GetNames ( ) . ToArray ( ) ;
88102
89- string pathToCerts = @"C:\etc\traefik\certs" ;
103+ IEnumerable < string > hostNamesKeys = keys . Where ( n => regex . IsMatch ( n ) ) ;
90104
91- switch ( topology )
105+ List < string > hostNames = new List < string > ( ) ;
106+
107+ foreach ( string hostName in hostNamesKeys )
92108 {
93- case Topology . Xm1 :
94- case Topology . Xp1 :
95- if ( args . Modules . Contains ( Module . Horizon ) )
96- {
97- return $@ "tls:
98- certificates:
99- - certFile: { pathToCerts } \{ args . EnvModel . CmHost } .crt
100- keyFile: { pathToCerts } \{ args . EnvModel . CmHost } .key
101- - certFile: { pathToCerts } \{ args . EnvModel . CdHost } .crt
102- keyFile: { pathToCerts } \{ args . EnvModel . CdHost } .key
103- - certFile: { pathToCerts } \{ args . EnvModel . IdHost } .crt
104- keyFile: { pathToCerts } \{ args . EnvModel . IdHost } .key
105- - certFile: { pathToCerts } \{ args . EnvModel . HorizonHost } .crt
106- keyFile: { pathToCerts } \{ args . EnvModel . HorizonHost } .key
107- " ;
108- }
109- return $@ "tls:
110- certificates:
111- - certFile: { pathToCerts } \{ args . EnvModel . CmHost } .crt
112- keyFile: { pathToCerts } \{ args . EnvModel . CmHost } .key
113- - certFile: { pathToCerts } \{ args . EnvModel . CdHost } .crt
114- keyFile: { pathToCerts } \{ args . EnvModel . CdHost } .key
115- - certFile: { pathToCerts } \{ args . EnvModel . IdHost } .crt
116- keyFile: { pathToCerts } \{ args . EnvModel . IdHost } .key
117- " ;
118- case Topology . Xp0 :
119- if ( args . Modules . Contains ( Module . Horizon ) )
120- {
121- return $@ "tls:
122- certificates:
123- - certFile: { pathToCerts } \{ args . EnvModel . CmHost } .crt
124- keyFile: { pathToCerts } \{ args . EnvModel . CmHost } .key
125- - certFile: { pathToCerts } \{ args . EnvModel . IdHost } .crt
126- keyFile: { pathToCerts } \{ args . EnvModel . IdHost } .key
127- - certFile: { pathToCerts } \{ args . EnvModel . HorizonHost } .crt
128- keyFile: { pathToCerts } \{ args . EnvModel . HorizonHost } .key
129- " ;
130- }
131- return $@ "tls:
132- certificates:
133- - certFile: { pathToCerts } \{ args . EnvModel . CmHost } .crt
134- keyFile: { pathToCerts } \{ args . EnvModel . CmHost } .key
135- - certFile: { pathToCerts } \{ args . EnvModel . IdHost } .crt
136- keyFile: { pathToCerts } \{ args . EnvModel . IdHost } .key
137- " ;
138- default :
139- throw new InvalidOperationException ( "Config is not defined for '" + topology . ToString ( ) + "' topology." ) ;
109+ hostNames . Add ( envModel [ hostName ] ) ;
140110 }
111+
112+ return hostNames ;
141113 }
142114
143- private void UpdateConfigFile ( string fileName , string content )
115+ private YamlDocument GenerateCertsConfigFile ( EnvModel envModel )
144116 {
145- File . WriteAllText ( fileName , content ) ;
117+ List < YamlNode > certificates = new List < YamlNode > ( ) ;
118+
119+ foreach ( string hostName in GetHostnames ( envModel ) )
120+ {
121+ certificates . Add ( new YamlMappingNode (
122+ new YamlScalarNode ( "certFile" ) , new YamlScalarNode ( $@ "{ PathToCerts } \{ hostName } .crt") ,
123+ new YamlScalarNode ( "keyFile" ) , new YamlScalarNode ( $@ "{ PathToCerts } \{ hostName } .key")
124+ ) ) ;
125+ }
126+
127+ return new YamlDocument (
128+ new YamlMappingNode (
129+ new YamlScalarNode ( "tls" ) , new YamlMappingNode (
130+ new YamlScalarNode ( "certificates" ) , new YamlSequenceNode ( certificates ) ) ) ) ;
146131 }
147132
148- protected virtual string GetScript ( InstallContainerArgs args )
133+ protected virtual string GetScript ( EnvModel envModel )
149134 {
150- Topology topology = args . Topology ;
135+ string template = string . Empty ;
151136
152- switch ( topology )
137+ foreach ( string hostName in GetHostnames ( envModel ) )
153138 {
154- case Topology . Xm1 :
155- case Topology . Xp1 :
156- if ( args . Modules . Contains ( Module . Horizon ) )
157- {
158- return GetXm1OrXp1AndHorizonScript ( args . EnvModel . CmHost , args . EnvModel . CdHost , args . EnvModel . IdHost , args . EnvModel . HorizonHost ) ;
159- }
160- return GetXm1OrXp1Script ( args . EnvModel . CmHost , args . EnvModel . CdHost , args . EnvModel . IdHost ) ;
161- case Topology . Xp0 :
162- if ( args . Modules . Contains ( Module . Horizon ) )
163- {
164- return GetXp0AndHorizonScript ( args . EnvModel . CmHost , args . EnvModel . IdHost , args . EnvModel . HorizonHost ) ;
165- }
166- return GetXp0Script ( args . EnvModel . CmHost , args . EnvModel . IdHost ) ;
167- default :
168- throw new InvalidOperationException ( "Generate certificates script cannot be resolved for '" + topology . ToString ( ) + "'" ) ;
139+ template += Environment . NewLine + $@ "mkcert -cert-file { PathToCertFolder } \{ hostName } .crt -key-file { PathToCertFolder } \{ hostName } .key ""{ hostName } """;
169140 }
141+
142+ return template;
170143 }
171144
172145 private void ExecuteScript(Func<Collection<PSObject>> p)
@@ -191,45 +164,5 @@ private void ExecuteScript(Func<Collection<PSObject>> p)
191164 throw ;
192165 }
193166 }
194-
195- private string GetXp0Script ( string cmHost , string idHost )
196- {
197- string template = @"
198- mkcert -cert-file {0}\{1}.crt -key-file {0}\{1}.key ""{1}""
199- mkcert -cert-file {0}\{2}.crt -key-file {0}\{2}.key ""{2}""" ;
200-
201- return string . Format ( template , PathToCertFolder , cmHost , idHost ) ;
202- }
203-
204- private string GetXm1OrXp1Script ( string cmHost , string cdHost , string idHost )
205- {
206- string template = @"
207- mkcert -cert-file {0}\{1}.crt -key-file {0}\{1}.key ""{1}""
208- mkcert -cert-file {0}\{2}.crt -key-file {0}\{2}.key ""{2}""
209- mkcert -cert-file {0}\{3}.crt -key-file {0}\{3}.key ""{3}""" ;
210-
211- return string . Format ( template , PathToCertFolder , cmHost , idHost , cdHost ) ;
212- }
213-
214- private string GetXp0AndHorizonScript ( string cmHost , string idHost , string hrzHost )
215- {
216- string template = @"
217- mkcert -cert-file {0}\{1}.crt -key-file {0}\{1}.key ""{1}""
218- mkcert -cert-file {0}\{2}.crt -key-file {0}\{2}.key ""{2}""
219- mkcert -cert-file {0}\{3}.crt -key-file {0}\{3}.key ""{3}""" ;
220-
221- return string . Format ( template , PathToCertFolder , cmHost , idHost , hrzHost ) ;
222- }
223-
224- private string GetXm1OrXp1AndHorizonScript ( string cmHost , string cdHost , string idHost , string hrzHost )
225- {
226- string template = @"
227- mkcert -cert-file {0}\{1}.crt -key-file {0}\{1}.key ""{1}""
228- mkcert -cert-file {0}\{2}.crt -key-file {0}\{2}.key ""{2}""
229- mkcert -cert-file {0}\{3}.crt -key-file {0}\{3}.key ""{3}""
230- mkcert -cert-file {0}\{4}.crt -key-file {0}\{4}.key ""{4}""" ;
231-
232- return string . Format ( template , PathToCertFolder , cmHost , idHost , cdHost , hrzHost ) ;
233- }
234167 }
235168}
0 commit comments