/
api.go
48 lines (38 loc) · 1.23 KB
/
api.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
package sessionfactory
import (
"github.com/Appliscale/cloud-security-audit/csasession"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// GetSession returns cached AWS session.
func (factory *SessionFactory) GetSession(config csasession.SessionConfig) (*session.Session, error) {
factory.mutex.Lock()
defer factory.mutex.Unlock()
if sess, ok := factory.regionToSession[config.Region]; ok {
return sess, nil
}
return factory.NewSession(config)
}
// NewSession creates a new session and caches it.
func (factory *SessionFactory) NewSession(config csasession.SessionConfig) (*session.Session, error) {
sess, err := csasession.CreateSession(config)
if err != nil {
return nil, err
}
factory.regionToSession[config.Region] = sess
return sess, nil
}
func (factory *SessionFactory) SetNormalizeBucketLocation(config csasession.SessionConfig) error {
sess, err := factory.GetSession(config)
if err != nil {
return err
}
sess.Handlers.Unmarshal.PushBackNamed(s3.NormalizeBucketLocationHandler)
return nil
}
func (factory *SessionFactory) ReinitialiseSession(config csasession.SessionConfig) (err error) {
factory.mutex.Lock()
defer factory.mutex.Unlock()
_, err = factory.NewSession(config)
return
}