github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/util/syscall_credentials.go (about)

     1  /*
     2  Copyright 2020 Google LLC
     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 util
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  	"syscall"
    24  
    25  	"github.com/pkg/errors"
    26  	"github.com/sirupsen/logrus"
    27  )
    28  
    29  func SyscallCredentials(userStr string) (*syscall.Credential, error) {
    30  	uid, gid, err := getUIDAndGIDFromString(userStr)
    31  	if err != nil {
    32  		return nil, errors.Wrap(err, "get uid/gid")
    33  	}
    34  
    35  	u, err := LookupUser(fmt.Sprint(uid))
    36  	if err != nil {
    37  		return nil, errors.Wrap(err, "lookup")
    38  	}
    39  	logrus.Infof("Util.Lookup returned: %+v", u)
    40  
    41  	// initiliaze empty
    42  	groups := []uint32{}
    43  
    44  	gidStr, err := groupIDs(u)
    45  	if err != nil {
    46  		return nil, errors.Wrap(err, "group ids for user")
    47  	}
    48  
    49  	for _, g := range gidStr {
    50  		i, err := strconv.ParseUint(g, 10, 32)
    51  		if err != nil {
    52  			return nil, errors.Wrap(err, "parseuint")
    53  		}
    54  
    55  		groups = append(groups, uint32(i))
    56  	}
    57  
    58  	if !(len(strings.Split(userStr, ":")) > 1) {
    59  		if u.Gid != "" {
    60  			gid, _ = getGID(u.Gid)
    61  		}
    62  	}
    63  
    64  	return &syscall.Credential{
    65  		Uid:    uid,
    66  		Gid:    gid,
    67  		Groups: groups,
    68  	}, nil
    69  }