-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathemail_configuration.go
129 lines (118 loc) · 3.93 KB
/
email_configuration.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 model
import (
"context"
"database/sql"
"encoding/json"
"errors"
"github.com/deepfence/ThreatMapper/deepfence_utils/encryption"
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
postgresqlDb "github.com/deepfence/ThreatMapper/deepfence_utils/postgresql/postgresql-db"
)
type EmailConfigurationSMTP struct {
EmailID string `json:"email_id" validate:"required,email"`
SMTP string `json:"smtp" validate:"required,min=3,max=128"`
Port string `json:"port" validate:"required,min=1,max=5,number"`
Password string `json:"password" validate:"required,min=3,max=128"`
}
type EmailConfigurationSES struct {
EmailID string `json:"email_id" validate:"required,email"`
AmazonAccessKey string `json:"amazon_access_key" validate:"required,min=16,max=128"`
AmazonSecretKey string `json:"amazon_secret_key" validate:"required,min=16,max=128"`
SesRegion string `json:"ses_region" validate:"required,oneof=us-east-1 us-east-2 us-west-1 us-west-2 af-south-1 ap-east-1 ap-south-1 ap-northeast-1 ap-northeast-2 ap-northeast-3 ap-southeast-1 ap-southeast-2 ap-southeast-3 ca-central-1 eu-central-1 eu-west-1 eu-west-2 eu-west-3 eu-south-1 eu-north-1 me-south-1 me-central-1 sa-east-1 us-gov-east-1 us-gov-west-1"`
}
type EmailConfigurationSendGrid struct {
EmailID string `json:"email_id" validate:"required,email"`
APIKey string `json:"apikey" validate:"required,min=3,max=128"`
}
type EmailConfigurationAdd struct {
EmailProvider string `json:"email_provider"`
CreatedByUserID int64 `json:"created_by_user_id"`
EmailID string `json:"email_id"`
SMTP string `json:"smtp"`
Port string `json:"port"`
Password string `json:"password"`
AmazonAccessKey string `json:"amazon_access_key"`
AmazonSecretKey string `json:"amazon_secret_key"`
SesRegion string `json:"ses_region"`
APIKey string `json:"apikey"`
}
type EmailConfigurationResp struct {
ID int64 `json:"id"`
EmailProvider string `json:"email_provider"`
CreatedByUserID int64 `json:"created_by_user_id"`
EmailID string `json:"email_id"`
SMTP string `json:"smtp"`
Port string `json:"port"`
SesRegion string `json:"ses_region"`
}
type ConfigIDPathReq struct {
ConfigID string `path:"config_id" validate:"required" required:"true"`
}
func (e *EmailConfigurationAdd) Create(ctx context.Context, pgClient *postgresqlDb.Queries) error {
_, err := pgClient.GetSetting(ctx, EmailConfigurationKey)
switch {
case errors.Is(err, sql.ErrNoRows):
// valid case
case err != nil:
log.Error().Msgf(err.Error())
return err
default:
return errors.New("email Configuration already exists")
}
aesValue, err := GetAESValueForEncryption(ctx, pgClient)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
// note: we'll encrypt the secret in registry interface object and use its secretgetter
// to map the secrets with req
aes := encryption.AES{}
err = json.Unmarshal(aesValue, &aes)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
if e.Password != "" {
e.Password, err = aes.Encrypt(e.Password)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
}
if e.AmazonAccessKey != "" {
e.AmazonAccessKey, err = aes.Encrypt(e.AmazonAccessKey)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
}
if e.AmazonSecretKey != "" {
e.AmazonSecretKey, err = aes.Encrypt(e.AmazonSecretKey)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
}
if e.APIKey != "" {
e.APIKey, err = aes.Encrypt(e.APIKey)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
}
settingVal, err := json.Marshal(*e)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
_, err = pgClient.CreateSetting(ctx, postgresqlDb.CreateSettingParams{
Key: EmailConfigurationKey,
Value: settingVal,
IsVisibleOnUi: false,
})
if err != nil {
log.Error().Msgf(err.Error())
return err
}
return nil
}