forked from awsdocs/aws-doc-sdk-examples
/
s3SetTls12.go
127 lines (106 loc) · 3.74 KB
/
s3SetTls12.go
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
// snippet-comment:[The snippet tags are for the AWS SDK for Go Developer Guide. Do not remove.]
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
// snippet-start:[s3.go.set_tls_12]
package main
import (
"crypto/tls"
"flag"
"fmt"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"golang.org/x/net/http2"
)
// GetTLSVersion gets the version of TLS you have configured
// snippet-start:[s3.go.get_tls_version]
func GetTLSVersion(tr *http.Transport) string {
switch tr.TLSClientConfig.MinVersion {
case tls.VersionTLS10:
return "TLS 1.0"
case tls.VersionTLS11:
return "TLS 1.1"
case tls.VersionTLS12:
return "TLS 1.2"
case tls.VersionTLS13:
return "TLS 1.3"
}
return "Unknown"
}
// snippet-end:[s3.go.get_tls_version]
// ConfirmBucketItemExists returns nil if the bucket and item can be accessed
func ConfirmBucketItemExists(sess *session.Session, bucket *string, item *string) error {
// snippet-start:[s3.go.set_tls_12_client]
svc := s3.New(sess)
_, err := svc.HeadObject(&s3.HeadObjectInput{
Bucket: bucket,
Key: item,
})
// snippet-end:[s3.go.set_tls_12_client]
if err != nil {
return err
}
return nil
}
func main() {
bucket := flag.String("b", "", "The bucket to check")
object := flag.String("o", "", "The bucket object to check")
region := flag.String("r", "us-west-2", "The region where the bucket lives")
goV112 := flag.Bool("v", false, "Whether the Go version is prior to 1.13")
flag.Parse()
if *bucket == "" || *object == "" {
fmt.Println("You must supply a bucket name (-b BUCKET) and item name (-o OBJECT)")
return
}
// snippet-start:[s3.go.set_tls_12_transport]
tr := &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}
// snippet-end:[s3.go.set_tls_12_transport]
if *goV112 {
// snippet-start:[s3.go.set_tls_12_cfg_112]
err := http2.ConfigureTransport(tr)
if err != nil {
fmt.Println("Got an error configuring HTTP transport")
fmt.Println(err)
return
}
// snippet-end:[s3.go.set_tls_12_cfg_112]
} else {
// snippet-start:[s3.go.set_tls_12_cfg_113]
tr.ForceAttemptHTTP2 = true
// snippet-end:[s3.go.set_tls_12_cfg_113]
}
// snippet-start:[s3.go.set_tls_12_session]
client := http.Client{Transport: tr}
sess := session.Must(session.NewSession(&aws.Config{
Region: region,
HTTPClient: &client,
}))
// snippet-end:[s3.go.set_tls_12_session]
// Confirm TLS version
// snippet-start:[s3.go.get_tls_version_call]
s3Client := s3.New(sess)
if tr, ok := s3Client.Config.HTTPClient.Transport.(*http.Transport); ok {
fmt.Println("Client uses " + GetTLSVersion(tr))
}
// snippet-end:[s3.go.get_tls_version_call]
err := ConfirmBucketItemExists(sess, bucket, object)
if err != nil {
fmt.Println("Could not confirm whether bucket and item exists")
return
}
fmt.Println("Bucket " + *bucket + " and item " + *object + " can be accessed")
}
// snippet-end:[s3.go.set_tls_12]