-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
129 lines (108 loc) · 3.32 KB
/
main.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
128
129
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"code.cloudfoundry.org/lager/v3"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
brokertags "github.com/cloud-gov/go-broker-tags"
cf "github.com/cloudfoundry-community/go-cfclient/v3/client"
cfconfig "github.com/cloudfoundry-community/go-cfclient/v3/config"
"github.com/pivotal-cf/brokerapi/v10"
"github.com/cloud-gov/s3-broker/awsiam"
"github.com/cloud-gov/s3-broker/awss3"
"github.com/cloud-gov/s3-broker/broker"
)
var (
configFilePath string
port string
logLevels = map[string]lager.LogLevel{
"DEBUG": lager.DEBUG,
"INFO": lager.INFO,
"ERROR": lager.ERROR,
"FATAL": lager.FATAL,
}
)
func init() {
flag.StringVar(&configFilePath, "config", "", "Location of the config file")
flag.StringVar(&port, "port", "3000", "Listen port")
}
func buildLogger(logLevel string) lager.Logger {
laggerLogLevel, ok := logLevels[strings.ToUpper(logLevel)]
if !ok {
log.Fatal("Invalid log level: ", logLevel)
}
logger := lager.NewLogger("s3-broker")
logger.RegisterSink(lager.NewWriterSink(os.Stdout, laggerLogLevel))
return logger
}
func main() {
flag.Parse()
config, err := LoadConfig(configFilePath)
if err != nil {
log.Fatalf("Error loading config file: %s", err)
}
logger := buildLogger(config.LogLevel)
awsConfig := aws.NewConfig().WithRegion(config.S3Config.Region)
if config.S3Config.Endpoint != "" {
fmt.Printf("Using alternate endpoint: %s\n", config.S3Config.Endpoint)
awsConfig.WithEndpoint(config.S3Config.Endpoint)
}
if config.S3Config.InsecureSkipVerify {
fmt.Printf("Setting connection to insecure (do not validate certificates)\n")
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: config.S3Config.InsecureSkipVerify}
customClient := &http.Client{Transport: customTransport}
awsConfig.WithHTTPClient(customClient)
}
awsSession := session.New(awsConfig)
s3svc := s3.New(awsSession)
s3bucket := awss3.NewS3Bucket(s3svc, logger)
user, err := awsiam.NewUser(config.S3Config.Provider, logger, awsSession, config.S3Config.Endpoint, config.S3Config.InsecureSkipVerify)
if err != nil {
log.Fatalf("Failure to configure user management: %s", err)
}
var client *cf.Client
if config.CFConfig != nil {
cfConfig, err := cfconfig.NewClientSecret(config.CFConfig.ApiAddress, config.CFConfig.ClientID, config.CFConfig.ClientSecret)
if err != nil {
log.Fatalf("Error creating CF config: %s", err)
}
client, err = cf.New(cfConfig)
if err != nil {
log.Fatalf("Error creating CF client: %s", err)
}
}
tagManager, err := brokertags.NewCFTagManager(
"S3 broker",
config.Environment,
config.CFConfig.ApiAddress,
config.CFConfig.ClientID,
config.CFConfig.ClientSecret,
)
if err != nil {
log.Fatalf("Failure to configure tag manager: %s", err)
}
serviceBroker := broker.New(
config.S3Config,
s3bucket,
user,
client,
logger,
tagManager,
)
credentials := brokerapi.BrokerCredentials{
Username: config.Username,
Password: config.Password,
}
brokerAPI := brokerapi.New(serviceBroker, logger, credentials)
http.Handle("/", brokerAPI)
fmt.Println("S3 Service Broker started on port " + port + "...")
http.ListenAndServe(":"+port, nil)
}