11package integration_test
22
33import (
4+ "crypto/tls"
5+ "crypto/x509"
46 "fmt"
5- "io/ioutil "
7+ "net/http "
68 "os"
79 "path/filepath"
810 "testing"
@@ -83,7 +85,7 @@ func testNoPackageManager(t *testing.T, context spec.G, it spec.S) {
8385 source , err = occam .Source (filepath .Join ("testdata" , "no_package_manager" ))
8486 Expect (err ).NotTo (HaveOccurred ())
8587
86- Expect (ioutil .WriteFile (filepath .Join (source , "Procfile" ),
88+ Expect (os .WriteFile (filepath .Join (source , "Procfile" ),
8789 []byte ("web: python hello.py" ), os .ModePerm )).
8890 To (Succeed ())
8991
@@ -98,14 +100,15 @@ func testNoPackageManager(t *testing.T, context spec.G, it spec.S) {
98100 Execute (name , source )
99101 Expect (err ).NotTo (HaveOccurred (), logs .String ())
100102
103+ Expect (logs ).To (ContainLines (ContainSubstring ("CA Certificates Buildpack" )))
101104 Expect (logs ).To (ContainLines (ContainSubstring ("CPython Buildpack" )))
102105 Expect (logs ).To (ContainLines (ContainSubstring ("Python Start Buildpack" )))
103106 Expect (logs ).To (ContainLines (ContainSubstring ("Procfile Buildpack" )))
104107 Expect (logs ).To (ContainLines (ContainSubstring ("Environment Variables Buildpack" )))
105108 Expect (logs ).To (ContainLines (ContainSubstring ("Image Labels Buildpack" )))
106109
107- Expect (image .Buildpacks [3 ].Key ).To (Equal ("paketo-buildpacks/environment-variables" ))
108- Expect (image .Buildpacks [3 ].Layers ["environment-variables" ].Metadata ["variables" ]).To (Equal (map [string ]interface {}{"SOME_VARIABLE" : "some-value" }))
110+ Expect (image .Buildpacks [4 ].Key ).To (Equal ("paketo-buildpacks/environment-variables" ))
111+ Expect (image .Buildpacks [4 ].Layers ["environment-variables" ].Metadata ["variables" ]).To (Equal (map [string ]interface {}{"SOME_VARIABLE" : "some-value" }))
109112 Expect (image .Labels ["some-label" ]).To (Equal ("some-value" ))
110113
111114 container , err = docker .Container .Run .Execute (image .ID )
@@ -118,5 +121,83 @@ func testNoPackageManager(t *testing.T, context spec.G, it spec.S) {
118121 }).Should (ContainSubstring ("Hello" ))
119122 })
120123 })
124+
125+ context ("when using CA certificates" , func () {
126+ var client * http.Client
127+
128+ it .Before (func () {
129+ var err error
130+ source , err = occam .Source (filepath .Join ("testdata" , "ca_cert_apps" ))
131+ Expect (err ).NotTo (HaveOccurred ())
132+
133+ caCert , err := os .ReadFile (filepath .Join (source , "client_certs" , "ca.pem" ))
134+ Expect (err ).NotTo (HaveOccurred ())
135+
136+ caCertPool := x509 .NewCertPool ()
137+ caCertPool .AppendCertsFromPEM (caCert )
138+
139+ cert , err := tls .LoadX509KeyPair (
140+ filepath .Join (source , "client_certs" , "cert.pem" ),
141+ filepath .Join (source , "client_certs" , "key.pem" ))
142+ Expect (err ).NotTo (HaveOccurred ())
143+
144+ client = & http.Client {
145+ Transport : & http.Transport {
146+ TLSClientConfig : & tls.Config {
147+ RootCAs : caCertPool ,
148+ Certificates : []tls.Certificate {cert },
149+ MinVersion : tls .VersionTLS12 ,
150+ },
151+ },
152+ }
153+ })
154+
155+ it ("builds a working OCI image with a start command and uses a client-side CA cert for requests" , func () {
156+ var err error
157+ var logs fmt.Stringer
158+
159+ image , logs , err = pack .WithNoColor ().Build .
160+ WithBuildpacks (pythonBuildpack ).
161+ WithPullPolicy ("never" ).
162+ Execute (name , filepath .Join (source , "no_package_manager" ))
163+ Expect (err ).NotTo (HaveOccurred ())
164+
165+ Expect (logs ).To (ContainLines (ContainSubstring ("CA Certificates Buildpack" )))
166+ Expect (logs ).To (ContainLines (ContainSubstring ("CPython Buildpack" )))
167+ Expect (logs ).To (ContainLines (ContainSubstring ("Python Start Buildpack" )))
168+ Expect (logs ).To (ContainLines (ContainSubstring ("Procfile Buildpack" )))
169+
170+ container , err = docker .Container .Run .
171+ WithPublish ("8080" ).
172+ WithEnv (map [string ]string {
173+ "PORT" : "8080" ,
174+ "SERVICE_BINDING_ROOT" : "/bindings" ,
175+ }).
176+ WithVolume (fmt .Sprintf ("%s:/bindings/ca-certificates" , filepath .Join (source , "bindings" ))).
177+ Execute (image .ID )
178+ Expect (err ).NotTo (HaveOccurred ())
179+
180+ Eventually (func () string {
181+ cLogs , err := docker .Container .Logs .Execute (container .ID )
182+ Expect (err ).NotTo (HaveOccurred ())
183+ return cLogs .String ()
184+ }).Should (
185+ ContainSubstring ("Added 1 additional CA certificate(s) to system truststore" ),
186+ )
187+
188+ request , err := http .NewRequest ("GET" , fmt .Sprintf ("https://localhost:%s" , container .HostPort ("8080" )), nil )
189+ Expect (err ).NotTo (HaveOccurred ())
190+
191+ var response * http.Response
192+ Eventually (func () error {
193+ var err error
194+ response , err = client .Do (request )
195+ return err
196+ }).Should (BeNil ())
197+ defer response .Body .Close ()
198+
199+ Expect (response .StatusCode ).To (Equal (http .StatusOK ))
200+ })
201+ })
121202 })
122203}
0 commit comments