| 1 |
/* |
| 2 |
Copyright 2014 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 tokenfile |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"encoding/csv" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"os" |
| 25 |
"strings" |
| 26 |
|
| 27 |
"k8s.io/apiserver/pkg/authentication/authenticator" |
| 28 |
"k8s.io/apiserver/pkg/authentication/user" |
| 29 |
"k8s.io/klog/v2" |
| 30 |
) |
| 31 |
|
| 32 |
type TokenAuthenticator struct { |
| 33 |
tokens map[string]*user.DefaultInfo |
| 34 |
} |
| 35 |
|
| 36 |
// New returns a TokenAuthenticator for a single token |
| 37 |
func New(tokens map[string]*user.DefaultInfo) *TokenAuthenticator { |
| 38 |
return &TokenAuthenticator{ |
| 39 |
tokens: tokens, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// NewCSV returns a TokenAuthenticator, populated from a CSV file. |
| 44 |
// The CSV file must contain records in the format "token,username,useruid" |
| 45 |
func NewCSV(path string) (*TokenAuthenticator, error) { |
| 46 |
file, err := os.Open(path) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
defer file.Close() |
| 51 |
|
| 52 |
recordNum := 0 |
| 53 |
tokens := make(map[string]*user.DefaultInfo) |
| 54 |
reader := csv.NewReader(file) |
| 55 |
reader.FieldsPerRecord = -1 |
| 56 |
for { |
| 57 |
record, err := reader.Read() |
| 58 |
if err == io.EOF { |
| 59 |
break |
| 60 |
} |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
if len(record) < 3 { |
| 65 |
return nil, fmt.Errorf("token file '%s' must have at least 3 columns (token, user name, user uid), found %d", path, len(record)) |
| 66 |
} |
| 67 |
|
| 68 |
recordNum++ |
| 69 |
if record[0] == "" { |
| 70 |
klog.Warningf("empty token has been found in token file '%s', record number '%d'", path, recordNum) |
| 71 |
continue |
| 72 |
} |
| 73 |
|
| 74 |
obj := &user.DefaultInfo{ |
| 75 |
Name: record[1], |
| 76 |
UID: record[2], |
| 77 |
} |
| 78 |
if _, exist := tokens[record[0]]; exist { |
| 79 |
klog.Warningf("duplicate token has been found in token file '%s', record number '%d'", path, recordNum) |
| 80 |
} |
| 81 |
tokens[record[0]] = obj |
| 82 |
|
| 83 |
if len(record) >= 4 { |
| 84 |
obj.Groups = strings.Split(record[3], ",") |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return &TokenAuthenticator{ |
| 89 |
tokens: tokens, |
| 90 |
}, nil |
| 91 |
} |
| 92 |
|
| 93 |
func (a *TokenAuthenticator) AuthenticateToken(ctx context.Context, value string) (*authenticator.Response, bool, error) { |
| 94 |
user, ok := a.tokens[value] |
| 95 |
if !ok { |
| 96 |
return nil, false, nil |
| 97 |
} |
| 98 |
return &authenticator.Response{User: user}, true, nil |
| 99 |
} |