|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "golang.org/x/crypto/bcrypt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + zerr "zotregistry.io/zot/errors" |
| 11 | +) |
| 12 | + |
| 13 | +type HtpasswdClient struct { |
| 14 | + credMap credMap |
| 15 | + filepath string |
| 16 | +} |
| 17 | + |
| 18 | +type credMap struct { |
| 19 | + m map[string]string |
| 20 | + rw *sync.RWMutex |
| 21 | +} |
| 22 | + |
| 23 | +func NewHtpasswdClient(filepath string) *HtpasswdClient { |
| 24 | + return &HtpasswdClient{ |
| 25 | + filepath: filepath, |
| 26 | + credMap: credMap{ |
| 27 | + m: make(map[string]string), |
| 28 | + rw: &sync.RWMutex{}, |
| 29 | + }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// Init initializes the HtpasswdClient. |
| 34 | +// It performs the file read using the filename specified in NewHtpasswdClient |
| 35 | +// and caches all user passwords. |
| 36 | +func (hc *HtpasswdClient) Init() error { |
| 37 | + credsFile, err := os.Open(hc.filepath) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("error occured while opening creds-file: %w", err) |
| 40 | + } |
| 41 | + defer credsFile.Close() |
| 42 | + |
| 43 | + hc.credMap.rw.Lock() |
| 44 | + defer hc.credMap.rw.Unlock() |
| 45 | + |
| 46 | + scanner := bufio.NewScanner(credsFile) |
| 47 | + for scanner.Scan() { |
| 48 | + line := scanner.Text() |
| 49 | + if strings.Contains(line, ":") { |
| 50 | + tokens := strings.Split(line, ":") |
| 51 | + if len(tokens) > 1 { |
| 52 | + hc.credMap.m[tokens[0]] = tokens[1] |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if err := scanner.Err(); err != nil { |
| 58 | + return fmt.Errorf("error occured while reading creds-file: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// Get returns the password associated with the login and a bool |
| 65 | +// indicating whether the login was found. |
| 66 | +// It does not check whether the user's password is correct. |
| 67 | +func (hc *HtpasswdClient) Get(login string) (string, bool) { |
| 68 | + return hc.credMap.Get(login) |
| 69 | +} |
| 70 | + |
| 71 | +// Set sets the new password. It does not perform any checks, |
| 72 | +// the only error is possible is encryption error. |
| 73 | +func (hc *HtpasswdClient) Set(login, password string) error { |
| 74 | + return hc.credMap.Set(login, password) |
| 75 | +} |
| 76 | + |
| 77 | +// CheckPassword checks whether the user has a specified password. |
| 78 | +// It returns an error if the user is not found or passwords do not match, |
| 79 | +// and returns the nil on passwords match. |
| 80 | +func (hc *HtpasswdClient) CheckPassword(login, password string) error { |
| 81 | + passwordHash, ok := hc.Get(login) |
| 82 | + if !ok { |
| 83 | + return zerr.ErrUserIsNotFound |
| 84 | + } |
| 85 | + |
| 86 | + err := bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(password)) |
| 87 | + if err != nil { |
| 88 | + return zerr.ErrPasswordsDoNotMatch |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +// ChangePassword changes the user password. |
| 95 | +// It accepts user login, his supposed old password for verification and new password. |
| 96 | +func (hc *HtpasswdClient) ChangePassword(login, supposedOldPassword, newPassword string) error { |
| 97 | + if len(newPassword) == 0 { |
| 98 | + return zerr.ErrPasswordIsEmpty |
| 99 | + } |
| 100 | + |
| 101 | + hc.credMap.rw.RLock() |
| 102 | + oldPassphrase, ok := hc.credMap.m[login] |
| 103 | + hc.credMap.rw.RUnlock() |
| 104 | + if !ok { |
| 105 | + return zerr.ErrUserIsNotFound |
| 106 | + } |
| 107 | + |
| 108 | + // given old password must match actual old password |
| 109 | + if err := bcrypt.CompareHashAndPassword([]byte(oldPassphrase), []byte(supposedOldPassword)); err != nil { |
| 110 | + return zerr.ErrOldPasswordIsWrong |
| 111 | + } |
| 112 | + |
| 113 | + // if passwords match, no need to update file and map, return nil as if operation is successful |
| 114 | + if err := bcrypt.CompareHashAndPassword([]byte(oldPassphrase), []byte(newPassword)); err == nil { |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + // encrypt new password |
| 119 | + newPassphrase, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("error occured while encrypting new password: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + file, err := os.ReadFile(hc.filepath) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("error occured while reading creds-file: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + // read passwords line by line to find the corresponding login |
| 130 | + lines := strings.Split(string(file), "\n") |
| 131 | + for i, line := range lines { |
| 132 | + if tokens := strings.SplitN(line, ":", 2); len(tokens) >= 2 { |
| 133 | + if tokens[0] == login { |
| 134 | + lines[i] = tokens[0] + ":" + string(newPassphrase) |
| 135 | + break |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // write new content to file |
| 141 | + output := strings.Join(lines, "\n") |
| 142 | + err = os.WriteFile(hc.filepath, []byte(output), 0644) |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("error occured while writing to creds-file: %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + // set to credMap only if all file operations are successful to prevent collisions |
| 148 | + hc.credMap.rw.Lock() |
| 149 | + hc.credMap.m[login] = string(newPassphrase) |
| 150 | + hc.credMap.rw.Unlock() |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func (c credMap) Set(login, password string) error { |
| 156 | + passphrase, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("error occured while cheking passwords: %w", err) |
| 159 | + } |
| 160 | + |
| 161 | + c.rw.Lock() |
| 162 | + c.m[login] = string(passphrase) |
| 163 | + c.rw.Unlock() |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +func (c credMap) Get(login string) (string, bool) { |
| 169 | + c.rw.RLock() |
| 170 | + defer c.rw.RUnlock() |
| 171 | + passphrase, ok := c.m[login] |
| 172 | + return passphrase, ok |
| 173 | +} |
0 commit comments