File: header.go

package info (click to toggle)
golang-k8s-sigs-apiserver-network-proxy 0.33.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,068 kB
  • sloc: makefile: 220; sh: 118
file content (93 lines) | stat: -rw-r--r-- 2,883 bytes parent folder | download
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Copyright 2019 The Kubernetes Authors.

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 header

import (
	"fmt"
	"net/url"
	"strconv"
)

const (
	ServerCount      = "serverCount"
	ServerID         = "serverID"
	AgentID          = "agentID"
	AgentIdentifiers = "agentIdentifiers"
	// AuthenticationTokenContextKey will be used as a key to store authentication tokens in grpc call
	// (https://tools.ietf.org/html/rfc6750#section-2.1)
	AuthenticationTokenContextKey = "Authorization"

	// AuthenticationTokenContextSchemePrefix has a prefix for auth token's content.
	// (https://tools.ietf.org/html/rfc6750#section-2.1)
	AuthenticationTokenContextSchemePrefix = "Bearer "

	// UserAgent is used to provide the client information in a proxy request
	UserAgent = "user-agent"
)

// Identifiers stores agent identifiers that will be used by the server when
// choosing agents
type Identifiers struct {
	IPv4         []string
	IPv6         []string
	Host         []string
	CIDR         []string
	DefaultRoute bool
}

type IdentifierType string

const (
	IPv4         IdentifierType = "ipv4"
	IPv6         IdentifierType = "ipv6"
	Host         IdentifierType = "host"
	CIDR         IdentifierType = "cidr"
	UID          IdentifierType = "uid"
	DefaultRoute IdentifierType = "default-route"
)

// GenAgentIdentifiers generates an Identifiers based on the input string, the
// input string should be a URL encoded mapping from IdentifierType to values.
func GenAgentIdentifiers(addrs string) (Identifiers, error) {
	var agentIdents Identifiers
	decoded, err := url.ParseQuery(addrs)
	if err != nil {
		return agentIdents, fmt.Errorf("fail to parse url encoded string: %v", err)
	}
	for idType, ids := range decoded {
		switch IdentifierType(idType) {
		case IPv4:
			agentIdents.IPv4 = append(agentIdents.IPv4, ids...)
		case IPv6:
			agentIdents.IPv6 = append(agentIdents.IPv6, ids...)
		case Host:
			agentIdents.Host = append(agentIdents.Host, ids...)
		case CIDR:
			agentIdents.CIDR = append(agentIdents.CIDR, ids...)
		case DefaultRoute:
			defaultRouteIdentifier, err := strconv.ParseBool(ids[0])
			if err == nil && defaultRouteIdentifier {
				agentIdents.DefaultRoute = true
			}
		default:
			// To support binary skew with agents that send new identifier type,
			// fail open. The better place to validate more strictly is within the agent.
			continue
		}
	}
	return agentIdents, nil
}