2018-04-13 12:31:03 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-04-13 12:31:03 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
package util
|
2018-04-13 12:31:03 +00:00
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
2018-04-13 12:31:03 +00:00
|
|
|
|
|
|
|
const (
|
2019-01-17 05:46:32 +00:00
|
|
|
credUserID = "userID"
|
2018-04-13 12:31:03 +00:00
|
|
|
credUserKey = "userKey"
|
2019-01-17 05:46:32 +00:00
|
|
|
credAdminID = "adminID"
|
2018-04-13 12:31:03 +00:00
|
|
|
credAdminKey = "adminKey"
|
2019-01-18 15:27:48 +00:00
|
|
|
credMonitors = "monitors"
|
2018-04-13 12:31:03 +00:00
|
|
|
)
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
type Credentials struct {
|
|
|
|
ID string
|
|
|
|
Key string
|
2018-04-13 12:31:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
func getCredentials(idField, keyField string, secrets map[string]string) (*Credentials, error) {
|
2018-04-13 12:31:03 +00:00
|
|
|
var (
|
2019-06-01 21:26:42 +00:00
|
|
|
c = &Credentials{}
|
2018-04-13 12:31:03 +00:00
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
if c.ID, ok = secrets[idField]; !ok {
|
2018-04-13 12:31:03 +00:00
|
|
|
return nil, fmt.Errorf("missing ID field '%s' in secrets", idField)
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
if c.Key, ok = secrets[keyField]; !ok {
|
2018-04-13 12:31:03 +00:00
|
|
|
return nil, fmt.Errorf("missing key field '%s' in secrets", keyField)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
func GetUserCredentials(secrets map[string]string) (*Credentials, error) {
|
2019-01-17 05:46:32 +00:00
|
|
|
return getCredentials(credUserID, credUserKey, secrets)
|
2018-04-13 12:31:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
func GetAdminCredentials(secrets map[string]string) (*Credentials, error) {
|
2019-01-17 05:46:32 +00:00
|
|
|
return getCredentials(credAdminID, credAdminKey, secrets)
|
2018-04-13 12:31:03 +00:00
|
|
|
}
|
2019-01-18 15:27:48 +00:00
|
|
|
|
2019-06-01 21:26:42 +00:00
|
|
|
func GetMonValFromSecret(secrets map[string]string) (string, error) {
|
2019-01-18 15:27:48 +00:00
|
|
|
if mons, ok := secrets[credMonitors]; ok {
|
|
|
|
return mons, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("missing %q", credMonitors)
|
|
|
|
}
|