-
Notifications
You must be signed in to change notification settings - Fork 11
/
dependencies.go
136 lines (116 loc) · 3.17 KB
/
dependencies.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
130
131
132
133
134
135
136
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/jmoiron/sqlx"
"github.com/spf13/viper"
"go.uber.org/zap"
"github.com/cmsgov/easi-app/pkg/appconfig"
"github.com/cmsgov/easi-app/pkg/oktaapi"
"github.com/cmsgov/easi-app/pkg/storage"
)
// getResolverDependencies takes a Viper config and returns a Store and Logger object to be used
// by various resolver functions.
func getResolverDependencies(config *viper.Viper) (
*sqlx.DB,
*storage.Store,
*zap.Logger,
*oktaapi.ClientWrapper,
) {
// Create the logger
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
// Create LD Client, which is required for creating the store
// ldClient, err := ld.MakeCustomClient("fake", ld.Config{Offline: true}, 0)
// if err != nil {
// panic(err)
// }
oktaClient, oktaClientErr := oktaapi.NewClient(config.GetString(appconfig.OKTAAPIURL), config.GetString(appconfig.OKTAAPIToken))
if oktaClientErr != nil {
logger.Fatal("failed to create okta api client", zap.Error(oktaClientErr))
}
// Create the DB Config & Store
dbConfig := storage.DBConfig{
Host: config.GetString(appconfig.DBHostConfigKey),
Port: config.GetString(appconfig.DBPortConfigKey),
Database: config.GetString(appconfig.DBNameConfigKey),
Username: config.GetString(appconfig.DBUsernameConfigKey),
Password: config.GetString(appconfig.DBPasswordConfigKey),
SSLMode: config.GetString(appconfig.DBSSLModeConfigKey),
MaxConnections: config.GetInt(appconfig.DBMaxConnections),
}
store, err := storage.NewStore(dbConfig, nil)
if err != nil {
fmt.Printf("Failed to get new database: %v", err)
panic(err)
}
db, err := newDB(dbConfig)
if err != nil {
fmt.Printf("Failed to get new database: %v", err)
panic(err)
}
return db, store, logger, oktaClient
}
func newDB(config storage.DBConfig) (*sqlx.DB, error) {
var db *sqlx.DB
var err error
if config.UseIAM {
// Connect using the IAM DB package
sess := session.Must(session.NewSession())
db = newConnectionPoolWithIam(sess, config)
err = db.Ping()
if err != nil {
return nil, err
}
} else {
// Connect via normal user/pass
dataSourceName := fmt.Sprintf(
"host=%s port=%s user=%s "+
"password=%s dbname=%s sslmode=%s",
config.Host,
config.Port,
config.Username,
config.Password,
config.Database,
config.SSLMode,
)
db, err = sqlx.Connect("postgres", dataSourceName)
if err != nil {
return nil, err
}
}
db.SetMaxOpenConns(config.MaxConnections)
return db, nil
}
func writeObjectToJSONFile(object interface{}, path string) {
entryBytes, err := json.Marshal(object)
if err != nil {
panic("Can't serialize the object")
}
file, err := os.Create(filepath.Clean(path))
if err != nil {
panic("Can't create the file")
}
_, err = file.Write(entryBytes)
if err != nil {
panic("Can't write the file")
}
}
func readJSONFromFile[anyType interface{}](file string, obj *anyType) error {
f, err := os.Open(file) //nolint
if err != nil {
log.Fatal(err)
return err
}
defer f.Close() //nolint
byteValue, _ := io.ReadAll(f)
err = json.Unmarshal(byteValue, &obj)
return err
}