Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package api |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
| 22 | "fmt" |
Lorenz Brun | a4ea9d0 | 2019-10-31 11:40:30 +0100 | [diff] [blame] | 23 | |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 24 | schema "git.monogon.dev/source/nexantic.git/core/generated/api" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | MinNameLength = 3 |
| 29 | ) |
| 30 | |
| 31 | var ( |
| 32 | ErrInvalidProvisioningToken = errors.New("invalid provisioning token") |
| 33 | ErrInvalidNameLength = fmt.Errorf("name must be at least %d characters long", MinNameLength) |
| 34 | ) |
| 35 | |
| 36 | func (s *Server) Setup(c context.Context, r *schema.SetupRequest) (*schema.SetupResponse, error) { |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 37 | return &schema.SetupResponse{}, nil |
| 38 | } |
| 39 | |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 40 | func (s *Server) BootstrapNewCluster(context.Context, *schema.BootstrapNewClusterRequest) (*schema.BootstrapNewClusterResponse, error) { |
| 41 | err := s.setupService.SetupNewCluster() |
| 42 | return &schema.BootstrapNewClusterResponse{}, err |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 45 | func (s *Server) JoinCluster(ctx context.Context, req *schema.JoinClusterRequest) (*schema.JoinClusterResponse, error) { |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 46 | // Verify provisioning token |
| 47 | if s.setupService.GetJoinClusterToken() != req.ProvisioningToken { |
| 48 | return nil, ErrInvalidProvisioningToken |
| 49 | } |
| 50 | |
| 51 | // Join cluster |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 52 | err := s.setupService.JoinCluster(req.InitialCluster, req.Certs) |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 57 | return &schema.JoinClusterResponse{}, nil |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func (s *Server) Attest(c context.Context, r *schema.AttestRequest) (*schema.AttestResponse, error) { |
| 61 | // TODO implement |
| 62 | return &schema.AttestResponse{ |
| 63 | Response: r.Challenge, |
| 64 | }, nil |
| 65 | } |