This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtoken.go
53 lines (46 loc) · 1.68 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/rand"
"encoding/base64"
)
// Token represents the communication between the process inside the VM and the
// host.
// In the case of clear containers, the shim process will use that token to
// identify itself against the proxy.
type Token string
const nilToken = Token("")
// generateRandomBytes returns securely generated random bytes.
//
// It will return an error if the system's secure random number generator
// fails to function correctly, in which case the caller should not continue.
func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
// GenerateToken returns a URL-safe, base64 encoded securely generated random
// string.
//
// It will return an error if the system's secure random number generator
// fails to function correctly, in which case the caller should not continue.
func GenerateToken(s int) (Token, error) {
b, err := generateRandomBytes(s)
return Token(base64.URLEncoding.EncodeToString(b)), err
}