github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/config/secret/secret.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package secret
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io/ioutil"
    23  )
    24  
    25  // LoadSecrets loads multiple paths of secrets and add them in a map.
    26  func LoadSecrets(paths []string) (map[string][]byte, error) {
    27  	secretsMap := make(map[string][]byte, len(paths))
    28  
    29  	for _, path := range paths {
    30  		secretValue, err := LoadSingleSecret(path)
    31  		if err != nil {
    32  			return nil, err
    33  		}
    34  		secretsMap[path] = secretValue
    35  	}
    36  	return secretsMap, nil
    37  }
    38  
    39  // LoadSingleSecret reads and returns the value of a single file.
    40  func LoadSingleSecret(path string) ([]byte, error) {
    41  	b, err := ioutil.ReadFile(path)
    42  	if err != nil {
    43  		return nil, fmt.Errorf("error reading %s: %v", path, err)
    44  	}
    45  	return bytes.TrimSpace(b), nil
    46  }