forked from canonical/iot-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
natspassword.go
158 lines (130 loc) · 3.96 KB
/
natspassword.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package configure
import (
"crypto/md5"
"errors"
"fmt"
"github.com/everactive/iot-agent/config"
"github.com/everactive/iot-agent/iotauth"
"github.com/everactive/iot-agent/pkg/nats"
"github.com/everactive/iot-agent/pkg/snapconfig"
"io/fs"
"os"
"path"
)
const (
firstRunBreadcrumbFilename = ".nats-auth-first-generation"
)
type NATSPasswordProcessor struct {
SnapDataDirectory string
PreviousAuthConfigFilePath string
FirstAuthGenBreadCrumbPath string
}
func (p NATSPasswordProcessor) New(snapDataDirectory string) *NATSPasswordProcessor {
return &NATSPasswordProcessor{
SnapDataDirectory: snapDataDirectory,
PreviousAuthConfigFilePath: path.Join(snapDataDirectory, "auth", "auth.json"),
FirstAuthGenBreadCrumbPath: path.Join(snapDataDirectory, firstRunBreadcrumbFilename),
}
}
func (p *NATSPasswordProcessor) isFirstRun() bool {
if _, err := os.Stat(p.FirstAuthGenBreadCrumbPath); errors.Is(err, fs.ErrNotExist) {
return true
}
return false
}
func (p *NATSPasswordProcessor) firstRun() bool {
if _, err := os.Stat(p.PreviousAuthConfigFilePath); err == nil {
err := os.Remove(p.PreviousAuthConfigFilePath)
if err != nil {
fmt.Printf("%s\n", err.Error())
}
}
file, err := os.Create(p.FirstAuthGenBreadCrumbPath)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Printf("%s\n", err.Error())
}
}(file)
legacyPasswordValue := natsutils.LegacySnapdAPIDefaultPassword
// If an existing password was set (a custom one), then we need to use it
// when we generate our new auth files, otherwise it was the default we shipped
existingPassword, err := snapconfig.GetConfigValue(fmt.Sprintf(config.NewSnapdPasswordKey, "snapd"))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
if len(existingPassword) > 0 {
legacyPasswordValue = existingPassword
}
userpassKV := map[string]string{
natsutils.LegacySnapdAPIUsername: legacyPasswordValue,
natsutils.SnapdAPIUsername: natsutils.SnapdAPIDefaultPassword,
}
iotauth.GenerateSnapdAuthFiles(userpassKV)
return true
}
func (p *NATSPasswordProcessor) getNewPassword(user string) *string {
password, err := snapconfig.GetConfigValue(fmt.Sprintf(config.NewSnapdPasswordKey, user))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
if len(password) < 22 {
return nil
}
hashValue, err := snapconfig.GetConfigValue(fmt.Sprintf(config.SnapdPasswordHash, user))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
if len(hashValue) > 0 {
// need to compare the hash values and see if this is actually a "new" password"
newHashValue := fmt.Sprintf("%x", md5.Sum([]byte(password)))
if hashValue != newHashValue {
err := snapconfig.SetConfigValue(fmt.Sprintf(config.SnapdPasswordHash, user), newHashValue)
if err != nil {
os.Exit(1)
}
return &password
}
}
return &password
}
func (p *NATSPasswordProcessor) normalConfiguration() bool {
userpassKV := map[string]string{}
// is a new snapd password set?
newSnapdPassword := p.getNewPassword(natsutils.LegacySnapdAPIUsername)
if newSnapdPassword != nil {
userpassKV[natsutils.LegacySnapdAPIUsername] = *newSnapdPassword
}
// is a new snapd1 password set?
newSnapd1Password := p.getNewPassword(natsutils.SnapdAPIUsername)
if newSnapd1Password != nil {
userpassKV[natsutils.SnapdAPIUsername] = *newSnapd1Password
}
if len(userpassKV) > 0 {
iotauth.GenerateSnapdAuthFiles(userpassKV)
// Update / set the hash value for the password so we know if it changed
for user, password := range userpassKV {
hashedPassword := md5.Sum([]byte(password))
err := snapconfig.SetConfigValue(fmt.Sprintf(config.SnapdPasswordHash, user), fmt.Sprintf("%x", hashedPassword))
if err != nil {
os.Exit(1)
}
}
return true
}
return false
}
func (p *NATSPasswordProcessor) HandleNATSPassword() bool {
if p.isFirstRun() {
return p.firstRun()
} else {
return p.normalConfiguration()
}
}