crossplane

Форк
0
/
logout.go 
85 строк · 2.4 Кб
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 xpkg
18

19
import (
20
	"context"
21
	"fmt"
22
	"net/http"
23

24
	"github.com/alecthomas/kong"
25
	"github.com/upbound/up-sdk-go"
26

27
	"github.com/crossplane/crossplane-runtime/pkg/errors"
28

29
	"github.com/crossplane/crossplane/internal/xpkg/upbound"
30
)
31

32
const (
33
	logoutPath = "/v1/logout"
34

35
	errLogoutFailed      = "unable to logout"
36
	errRemoveTokenFailed = "failed to remove token"
37
)
38

39
// AfterApply sets default values in login after assignment and validation.
40
func (c *logoutCmd) AfterApply(kongCtx *kong.Context) error {
41
	upCtx, err := upbound.NewFromFlags(c.Flags)
42
	if err != nil {
43
		return err
44
	}
45
	kongCtx.Bind(upCtx)
46
	cfg, err := upCtx.BuildSDKConfig()
47
	if err != nil {
48
		return err
49
	}
50
	c.client = cfg.Client
51
	return nil
52
}
53

54
// logoutCmd invalidates a stored session token for a given profile.
55
type logoutCmd struct {
56
	// Common Upbound API configuration
57
	Flags upbound.Flags `embed:""`
58

59
	// Internal state. These aren't part of the user-exposed CLI structure.
60
	client up.Client
61
}
62

63
// Run executes the logout command.
64
func (c *logoutCmd) Run(k *kong.Context, upCtx *upbound.Context) error {
65
	ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
66
	defer cancel()
67
	req, err := c.client.NewRequest(ctx, http.MethodPost, logoutPath, "", nil)
68
	if err != nil {
69
		return errors.Wrap(err, errLogoutFailed)
70
	}
71
	if err := c.client.Do(req, nil); err != nil {
72
		return errors.Wrap(err, errLogoutFailed)
73
	}
74
	// Logout is successful, remove token from config and update.
75
	upCtx.Profile.Session = ""
76
	if err := upCtx.Cfg.AddOrUpdateUpboundProfile(upCtx.ProfileName, upCtx.Profile); err != nil {
77
		return errors.Wrap(err, errRemoveTokenFailed)
78
	}
79
	if err := upCtx.CfgSrc.UpdateConfig(upCtx.Cfg); err != nil {
80
		return errors.Wrap(err, "failed to update config file")
81
	}
82

83
	_, _ = fmt.Fprintf(k.Stdout, "%s logged out.\n", upCtx.Profile.ID)
84
	return nil
85
}
86

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

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

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

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