1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
package session
import (
"bytes"
"fmt"
"net"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/awstesting"
)
var TLSBundleCertFile string
var TLSBundleKeyFile string
var TLSBundleCAFile string
func TestMain(m *testing.M) {
var err error
TLSBundleCertFile, TLSBundleKeyFile, TLSBundleCAFile, err = awstesting.CreateTLSBundleFiles()
if err != nil {
panic(err)
}
fmt.Println("TestMain", TLSBundleCertFile, TLSBundleKeyFile)
code := m.Run()
err = awstesting.CleanupTLSBundleFiles(TLSBundleCertFile, TLSBundleKeyFile, TLSBundleCAFile)
if err != nil {
panic(err)
}
os.Exit(code)
}
func TestNewSession_WithCustomCABundle_Env(t *testing.T) {
restoreEnvFn := initSessionTestEnv()
defer restoreEnvFn()
endpoint, err := awstesting.CreateTLSServer(TLSBundleCertFile, TLSBundleKeyFile, nil)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
os.Setenv("AWS_CA_BUNDLE", TLSBundleCAFile)
s, err := NewSession(&aws.Config{
HTTPClient: &http.Client{},
Endpoint: aws.String(endpoint),
Region: aws.String("mock-region"),
Credentials: credentials.AnonymousCredentials,
})
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if s == nil {
t.Fatalf("expect session to be created, got none")
}
req, _ := http.NewRequest("GET", *s.Config.Endpoint, nil)
resp, err := s.Config.HTTPClient.Do(req)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := http.StatusOK, resp.StatusCode; e != a {
t.Errorf("expect %d status code, got %d", e, a)
}
}
func TestNewSession_WithCustomCABundle_EnvNotExists(t *testing.T) {
restoreEnvFn := initSessionTestEnv()
defer restoreEnvFn()
os.Setenv("AWS_CA_BUNDLE", "file-not-exists")
s, err := NewSession()
if err == nil {
t.Fatalf("expect error, got none")
}
if e, a := "LoadCustomCABundleError", err.(awserr.Error).Code(); e != a {
t.Errorf("expect %s error code, got %s", e, a)
}
if s != nil {
t.Errorf("expect nil session, got %v", s)
}
}
func TestNewSession_WithCustomCABundle_Option(t *testing.T) {
restoreEnvFn := initSessionTestEnv()
defer restoreEnvFn()
endpoint, err := awstesting.CreateTLSServer(TLSBundleCertFile, TLSBundleKeyFile, nil)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
s, err := NewSessionWithOptions(Options{
Config: aws.Config{
HTTPClient: &http.Client{},
Endpoint: aws.String(endpoint),
Region: aws.String("mock-region"),
Credentials: credentials.AnonymousCredentials,
},
CustomCABundle: bytes.NewReader(awstesting.TLSBundleCA),
})
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if s == nil {
t.Fatalf("expect session to be created, got none")
}
req, _ := http.NewRequest("GET", *s.Config.Endpoint, nil)
resp, err := s.Config.HTTPClient.Do(req)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := http.StatusOK, resp.StatusCode; e != a {
t.Errorf("expect %d status code, got %d", e, a)
}
}
func TestNewSession_WithCustomCABundle_HTTPProxyAvailable(t *testing.T) {
restoreEnvFn := initSessionTestEnv()
defer restoreEnvFn()
s, err := NewSessionWithOptions(Options{
Config: aws.Config{
HTTPClient: &http.Client{},
Region: aws.String("mock-region"),
Credentials: credentials.AnonymousCredentials,
},
CustomCABundle: bytes.NewReader(awstesting.TLSBundleCA),
})
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if s == nil {
t.Fatalf("expect session to be created, got none")
}
tr := s.Config.HTTPClient.Transport.(*http.Transport)
if tr.Proxy == nil {
t.Fatalf("expect transport proxy, was nil")
}
if tr.TLSClientConfig.RootCAs == nil {
t.Fatalf("expect TLS config to have root CAs")
}
}
func TestNewSession_WithCustomCABundle_OptionPriority(t *testing.T) {
restoreEnvFn := initSessionTestEnv()
defer restoreEnvFn()
endpoint, err := awstesting.CreateTLSServer(TLSBundleCertFile, TLSBundleKeyFile, nil)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
os.Setenv("AWS_CA_BUNDLE", "file-not-exists")
s, err := NewSessionWithOptions(Options{
Config: aws.Config{
HTTPClient: &http.Client{},
Endpoint: aws.String(endpoint),
Region: aws.String("mock-region"),
Credentials: credentials.AnonymousCredentials,
},
CustomCABundle: bytes.NewReader(awstesting.TLSBundleCA),
})
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if s == nil {
t.Fatalf("expect session to be created, got none")
}
req, _ := http.NewRequest("GET", *s.Config.Endpoint, nil)
resp, err := s.Config.HTTPClient.Do(req)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := http.StatusOK, resp.StatusCode; e != a {
t.Errorf("expect %d status code, got %d", e, a)
}
}
type mockRoundTripper struct{}
func (m *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, nil
}
func TestNewSession_WithCustomCABundle_UnsupportedTransport(t *testing.T) {
restoreEnvFn := initSessionTestEnv()
defer restoreEnvFn()
s, err := NewSessionWithOptions(Options{
Config: aws.Config{
HTTPClient: &http.Client{
Transport: &mockRoundTripper{},
},
},
CustomCABundle: bytes.NewReader(awstesting.TLSBundleCA),
})
if err == nil {
t.Fatalf("expect error, got none")
}
if e, a := "LoadCustomCABundleError", err.(awserr.Error).Code(); e != a {
t.Errorf("expect %s error code, got %s", e, a)
}
if s != nil {
t.Errorf("expect nil session, got %v", s)
}
aerrMsg := err.(awserr.Error).Message()
if e, a := "transport unsupported type", aerrMsg; !strings.Contains(a, e) {
t.Errorf("expect %s to be in %s", e, a)
}
}
func TestNewSession_WithCustomCABundle_TransportSet(t *testing.T) {
restoreEnvFn := initSessionTestEnv()
defer restoreEnvFn()
endpoint, err := awstesting.CreateTLSServer(TLSBundleCertFile, TLSBundleKeyFile, nil)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
s, err := NewSessionWithOptions(Options{
Config: aws.Config{
Endpoint: aws.String(endpoint),
Region: aws.String("mock-region"),
Credentials: credentials.AnonymousCredentials,
HTTPClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).Dial,
TLSHandshakeTimeout: 2 * time.Second,
},
},
},
CustomCABundle: bytes.NewReader(awstesting.TLSBundleCA),
})
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if s == nil {
t.Fatalf("expect session to be created, got none")
}
req, _ := http.NewRequest("GET", *s.Config.Endpoint, nil)
resp, err := s.Config.HTTPClient.Do(req)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := http.StatusOK, resp.StatusCode; e != a {
t.Errorf("expect %d status code, got %d", e, a)
}
}
|