-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathdatabase_credentials.go
More file actions
121 lines (103 loc) · 3.46 KB
/
database_credentials.go
File metadata and controls
121 lines (103 loc) · 3.46 KB
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
package mysql
import (
"context"
"fmt"
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/importer"
"github.com/1Password/shell-plugins/sdk/provision"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/1Password/shell-plugins/sdk/schema/credname"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)
func DatabaseCredentials() schema.CredentialType {
return schema.CredentialType{
Name: credname.DatabaseCredentials,
DocsURL: sdk.URL("https://dev.mysql.com/doc/refman/en/connecting.html"),
Fields: []schema.CredentialField{
{
Name: fieldname.Host,
MarkdownDescription: "MySQL host to connect to.",
Optional: true,
},
{
Name: fieldname.Port,
MarkdownDescription: "Port used to connect to MySQL.",
Optional: true,
},
{
Name: fieldname.User,
MarkdownDescription: "MySQL user to authenticate as.",
Optional: true,
},
{
Name: fieldname.Password,
MarkdownDescription: "Password used to authenticate to MySQL.",
Secret: true,
},
{
Name: fieldname.Database,
MarkdownDescription: "Database name to connect to.",
Optional: true,
},
},
DefaultProvisioner: provision.TempFile(mysqlConfig, provision.Filename("my.cnf"), provision.PrependArgs("--defaults-file={{ .Path }}")),
Importer: importer.TryAll(
TryMySQLConfigFile("/etc/my.cnf"),
TryMySQLConfigFile("/etc/mysql/my.cnf"),
TryMySQLConfigFile("~/.my.cnf"),
TryMySQLConfigFile("~/.mylogin.cnf"),
),
}
}
func mysqlConfig(in sdk.ProvisionInput) ([]byte, error) {
content := "[client]\n"
if user, ok := in.ItemFields[fieldname.User]; ok {
content += configFileEntry("user", user)
}
if password, ok := in.ItemFields[fieldname.Password]; ok {
content += configFileEntry("password", password)
}
if host, ok := in.ItemFields[fieldname.Host]; ok {
content += configFileEntry("host", host)
}
if port, ok := in.ItemFields[fieldname.Port]; ok {
content += configFileEntry("port", port)
}
if database, ok := in.ItemFields[fieldname.Database]; ok {
content += configFileEntry("database", database)
}
return []byte(content), nil
}
func TryMySQLConfigFile(path string) sdk.Importer {
return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
credentialsFile, err := contents.ToINI()
if err != nil {
out.AddError(err)
return
}
fields := make(map[sdk.FieldName]string)
for _, section := range credentialsFile.Sections() {
if section.HasKey("user") && section.Key("user").Value() != "" {
fields[fieldname.User] = section.Key("user").Value()
}
if section.HasKey("password") && section.Key("password").Value() != "" {
fields[fieldname.Password] = section.Key("password").Value()
}
if section.HasKey("database") && section.Key("database").Value() != "" {
fields[fieldname.Database] = section.Key("database").Value()
}
if section.HasKey("host") && section.Key("host").Value() != "" {
fields[fieldname.Host] = section.Key("host").Value()
}
if section.HasKey("port") && section.Key("port").Value() != "" {
fields[fieldname.Port] = section.Key("port").Value()
}
}
out.AddCandidate(sdk.ImportCandidate{
Fields: fields,
})
})
}
func configFileEntry(key string, value string) string {
return fmt.Sprintf("%s=%s\n", key, value)
}