login.go 1.4 KB
Newer Older
D
dogsheng 已提交
1
// Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
2 3 4 5
// iSulad-img licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
6 7 8
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
9
// See the Mulan PSL v2 for more details.
O
overweight 已提交
10 11 12 13 14 15 16
// Description: iSulad login kit
// Author: wangfengtu
// Create: 2019-06-17

package main

import (
D
dogsheng 已提交
17
	"context"
O
overweight 已提交
18 19 20 21 22 23 24
	"strings"

	"github.com/containers/image/docker"
	"github.com/containers/image/pkg/docker/config"
	"github.com/containers/image/types"
)

D
dogsheng 已提交
25 26
func loginRegistry(gopts *globalOptions, sys *types.SystemContext, username string, password string, server string) error {
	svc, err := getImageService(gopts)
O
overweight 已提交
27 28 29 30
	if err != nil {
		return err
	}

D
dogsheng 已提交
31
	serverAddr := strings.Split(server, "/")[0]
O
overweight 已提交
32 33 34 35
	if secure := svc.IsSecureIndex(serverAddr); !secure {
		sys.DockerInsecureSkipTLSVerify = types.NewOptionalBool(true)
	}

D
dogsheng 已提交
36
	if err := docker.CheckAuth(context.Background(), sys, username, password, serverAddr); err != nil {
O
overweight 已提交
37 38 39 40 41 42 43 44 45
		return err
	}

	if err := config.SetAuthentication(sys, serverAddr, username, password); err != nil {
		return err
	}

	return nil
}