| 1 |
/* |
| 2 |
Copyright 2019 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 header |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"net/url" |
| 22 |
"strconv" |
| 23 |
) |
| 24 |
|
| 25 |
const ( |
| 26 |
ServerCount = "serverCount" |
| 27 |
ServerID = "serverID" |
| 28 |
AgentID = "agentID" |
| 29 |
AgentIdentifiers = "agentIdentifiers" |
| 30 |
// AuthenticationTokenContextKey will be used as a key to store authentication tokens in grpc call |
| 31 |
// (https://tools.ietf.org/html/rfc6750#section-2.1) |
| 32 |
AuthenticationTokenContextKey = "Authorization" |
| 33 |
|
| 34 |
// AuthenticationTokenContextSchemePrefix has a prefix for auth token's content. |
| 35 |
// (https://tools.ietf.org/html/rfc6750#section-2.1) |
| 36 |
AuthenticationTokenContextSchemePrefix = "Bearer " |
| 37 |
|
| 38 |
// UserAgent is used to provide the client information in a proxy request |
| 39 |
UserAgent = "user-agent" |
| 40 |
) |
| 41 |
|
| 42 |
// Identifiers stores agent identifiers that will be used by the server when |
| 43 |
// choosing agents |
| 44 |
type Identifiers struct { |
| 45 |
IPv4 []string |
| 46 |
IPv6 []string |
| 47 |
Host []string |
| 48 |
CIDR []string |
| 49 |
DefaultRoute bool |
| 50 |
} |
| 51 |
|
| 52 |
type IdentifierType string |
| 53 |
|
| 54 |
const ( |
| 55 |
IPv4 IdentifierType = "ipv4" |
| 56 |
IPv6 IdentifierType = "ipv6" |
| 57 |
Host IdentifierType = "host" |
| 58 |
CIDR IdentifierType = "cidr" |
| 59 |
UID IdentifierType = "uid" |
| 60 |
DefaultRoute IdentifierType = "default-route" |
| 61 |
) |
| 62 |
|
| 63 |
// GenAgentIdentifiers generates an Identifiers based on the input string, the |
| 64 |
// input string should be a URL encoded mapping from IdentifierType to values. |
| 65 |
func GenAgentIdentifiers(addrs string) (Identifiers, error) { |
| 66 |
var agentIdents Identifiers |
| 67 |
decoded, err := url.ParseQuery(addrs) |
| 68 |
if err != nil { |
| 69 |
return agentIdents, fmt.Errorf("fail to parse url encoded string: %v", err) |
| 70 |
} |
| 71 |
for idType, ids := range decoded { |
| 72 |
switch IdentifierType(idType) { |
| 73 |
case IPv4: |
| 74 |
agentIdents.IPv4 = append(agentIdents.IPv4, ids...) |
| 75 |
case IPv6: |
| 76 |
agentIdents.IPv6 = append(agentIdents.IPv6, ids...) |
| 77 |
case Host: |
| 78 |
agentIdents.Host = append(agentIdents.Host, ids...) |
| 79 |
case CIDR: |
| 80 |
agentIdents.CIDR = append(agentIdents.CIDR, ids...) |
| 81 |
case DefaultRoute: |
| 82 |
defaultRouteIdentifier, err := strconv.ParseBool(ids[0]) |
| 83 |
if err == nil && defaultRouteIdentifier { |
| 84 |
agentIdents.DefaultRoute = true |
| 85 |
} |
| 86 |
default: |
| 87 |
// To support binary skew with agents that send new identifier type, |
| 88 |
// fail open. The better place to validate more strictly is within the agent. |
| 89 |
continue |
| 90 |
} |
| 91 |
} |
| 92 |
return agentIdents, nil |
| 93 |
} |