crossplane

Форк
0
65 строк · 1.7 Кб
1
/*
2
Copyright 2023 The Crossplane 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 upbound
18

19
import (
20
	"encoding/json"
21
	"path/filepath"
22

23
	"github.com/spf13/afero"
24

25
	"github.com/crossplane/crossplane-runtime/pkg/errors"
26
)
27

28
const errInvalidTokenFile = "token file is invalid"
29

30
// TokenFile is the format in which Upbound tokens are stored on disk.
31
type TokenFile struct {
32
	AccessID string `json:"accessId"`
33
	Token    string `json:"token"`
34
}
35

36
// tokenConf is the configuration for obtaining a token.
37
type tokenConf struct {
38
	fs afero.Fs
39
}
40

41
// TokenOption modifies how a token is obtained.
42
type TokenOption func(conf *tokenConf)
43

44
// TokenFromPath extracts a token from the provided path.
45
func TokenFromPath(path string, opts ...TokenOption) (TokenFile, error) {
46
	conf := &tokenConf{
47
		fs: afero.NewOsFs(),
48
	}
49
	for _, o := range opts {
50
		o(conf)
51
	}
52
	tf := TokenFile{}
53
	f, err := conf.fs.Open(filepath.Clean(path))
54
	if err != nil {
55
		return tf, err
56
	}
57
	defer f.Close() //nolint:errcheck // we don't care about the error
58
	if err := json.NewDecoder(f).Decode(&tf); err != nil {
59
		return tf, err
60
	}
61
	if tf.AccessID == "" || tf.Token == "" {
62
		return tf, errors.New(errInvalidTokenFile)
63
	}
64
	return tf, nil
65
}
66

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.