forked from cloud-gov/uaa-credentials-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (74 loc) · 2.5 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"code.cloudfoundry.org/lager"
"github.com/cloudfoundry-community/go-cfclient"
"github.com/kelseyhightower/envconfig"
"github.com/pivotal-cf/brokerapi"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
type Config struct {
UAAAddress string `envconfig:"uaa_address" required:"true"`
UAAClientID string `envconfig:"uaa_client_id" required:"true"`
UAAClientSecret string `envconfig:"uaa_client_secret" required:"true"`
UAAZone string `envconfig:"uaa_zone" default:"uaa"`
CFAddress string `envconfig:"cf_address" required:"true"`
BrokerUsername string `envconfig:"broker_username" required:"true"`
BrokerPassword string `envconfig:"broker_password" required:"true"`
PasswordLength int `envconfig:"password_length" default:"32"`
EmailAddress string `envconfig:"email_address" required:"true"`
AccessTokenValidity int `envconfig:"access_token_validity" default:"600"`
RefreshTokenValidity int `envconfig:"refresh_token_validity" default:"86400"`
Port string `envconfig:"port" default:"3000"`
}
func NewClient(config Config) *http.Client {
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, http.DefaultClient)
cfg := &clientcredentials.Config{
ClientID: config.UAAClientID,
ClientSecret: config.UAAClientSecret,
TokenURL: fmt.Sprintf("%s/oauth/token", config.UAAAddress),
}
return cfg.Client(ctx)
}
func main() {
logger := lager.NewLogger("uaa-credentials-broker")
logger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.INFO))
config := Config{}
err := envconfig.Process("", &config)
if err != nil {
log.Fatalf("", err)
}
client := NewClient(config)
cfClient, err := cfclient.NewClient(&cfclient.Config{
ApiAddress: config.CFAddress,
ClientID: config.UAAClientID,
ClientSecret: config.UAAClientSecret,
})
if err != nil {
log.Fatalf("", err)
}
broker := DeployerAccountBroker{
logger: logger,
uaaClient: &UAAClient{
logger: logger,
endpoint: config.UAAAddress,
zone: config.UAAZone,
client: client,
},
cfClient: cfClient,
generatePassword: GenerateSecurePassword,
config: config,
}
credentials := brokerapi.BrokerCredentials{
Username: config.BrokerUsername,
Password: config.BrokerPassword,
}
brokerAPI := brokerapi.New(&broker, logger, credentials)
http.Handle("/", brokerAPI)
http.ListenAndServe(fmt.Sprintf(":%s", config.Port), nil)
}