|
| 1 | +/* |
| 2 | +Copyright 2023 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 do |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "net" |
| 24 | + "net/http" |
| 25 | + "os" |
| 26 | + "strconv" |
| 27 | + "strings" |
| 28 | + |
| 29 | + "github.com/digitalocean/godo" |
| 30 | + "golang.org/x/oauth2" |
| 31 | + "k8s.io/kops/pkg/bootstrap" |
| 32 | + "k8s.io/kops/pkg/wellknownports" |
| 33 | +) |
| 34 | + |
| 35 | +type DigitalOceanVerifierOptions struct { |
| 36 | +} |
| 37 | + |
| 38 | +type digitalOceanVerifier struct { |
| 39 | + doClient *godo.Client |
| 40 | +} |
| 41 | + |
| 42 | +var _ bootstrap.Verifier = &digitalOceanVerifier{} |
| 43 | + |
| 44 | +func NewVerifier(ctx context.Context, opt *DigitalOceanVerifierOptions) (bootstrap.Verifier, error) { |
| 45 | + accessToken := os.Getenv("DIGITALOCEAN_ACCESS_TOKEN") |
| 46 | + if accessToken == "" { |
| 47 | + return nil, errors.New("DIGITALOCEAN_ACCESS_TOKEN is required") |
| 48 | + } |
| 49 | + |
| 50 | + tokenSource := &TokenSource{ |
| 51 | + AccessToken: accessToken, |
| 52 | + } |
| 53 | + |
| 54 | + oauthClient := oauth2.NewClient(ctx, tokenSource) |
| 55 | + doClient := godo.NewClient(oauthClient) |
| 56 | + |
| 57 | + return &digitalOceanVerifier{ |
| 58 | + doClient: doClient, |
| 59 | + }, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (o digitalOceanVerifier) VerifyToken(ctx context.Context, rawRequest *http.Request, token string, body []byte, useInstanceIDForNodeName bool) (*bootstrap.VerifyResult, error) { |
| 63 | + if !strings.HasPrefix(token, DOAuthenticationTokenPrefix) { |
| 64 | + return nil, fmt.Errorf("incorrect authorization type") |
| 65 | + } |
| 66 | + serverIDString := strings.TrimPrefix(token, DOAuthenticationTokenPrefix) |
| 67 | + |
| 68 | + serverID, err := strconv.Atoi(serverIDString) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("invalid authorization token") |
| 71 | + } |
| 72 | + |
| 73 | + droplet, _, err := o.doClient.Droplets.Get(ctx, serverID) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("failed to get info for server %v: %w", token, err) |
| 76 | + } |
| 77 | + |
| 78 | + var addresses []string |
| 79 | + var challengeEndpoints []string |
| 80 | + if droplet.Networks != nil { |
| 81 | + for _, nic := range droplet.Networks.V4 { |
| 82 | + if nic.Type == "private" { |
| 83 | + addresses = append(addresses, nic.IPAddress) |
| 84 | + challengeEndpoints = append(challengeEndpoints, net.JoinHostPort(nic.IPAddress, strconv.Itoa(wellknownports.NodeupChallenge))) |
| 85 | + } |
| 86 | + } |
| 87 | + for _, nic := range droplet.Networks.V6 { |
| 88 | + if nic.Type == "private" { |
| 89 | + addresses = append(addresses, nic.IPAddress) |
| 90 | + challengeEndpoints = append(challengeEndpoints, net.JoinHostPort(nic.IPAddress, strconv.Itoa(wellknownports.NodeupChallenge))) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Note: we use TLS passthrough, so we lose the client IP address. |
| 96 | + // We therefore don't have a great way to verify the request. |
| 97 | + // We do at least prevent duplicate node registrations, preventing some attacks here. |
| 98 | + |
| 99 | + // The node challenge is important here though, verifying the caller has control of the IP address. |
| 100 | + |
| 101 | + nodeName := "" |
| 102 | + if len(addresses) == 0 { |
| 103 | + // Name seems a better default than the first IP, but we have to match what other components are expecting |
| 104 | + nodeName = droplet.Name |
| 105 | + } else { |
| 106 | + nodeName = addresses[0] |
| 107 | + } |
| 108 | + |
| 109 | + if len(challengeEndpoints) == 0 { |
| 110 | + return nil, fmt.Errorf("cannot determine challenge endpoint for server %q", serverID) |
| 111 | + } |
| 112 | + |
| 113 | + result := &bootstrap.VerifyResult{ |
| 114 | + NodeName: nodeName, |
| 115 | + CertificateNames: addresses, |
| 116 | + ChallengeEndpoint: challengeEndpoints[0], |
| 117 | + } |
| 118 | + |
| 119 | + for _, tag := range droplet.Tags { |
| 120 | + if strings.HasPrefix(tag, TagKubernetesInstanceGroup+":") { |
| 121 | + result.InstanceGroupName = strings.TrimPrefix(tag, TagKubernetesInstanceGroup+":") |
| 122 | + } |
| 123 | + } |
| 124 | + return result, nil |
| 125 | +} |
0 commit comments