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 node |
| 18 | |
| 19 | import ( |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 20 | "context" |
| 21 | |
Lorenz Brun | a4ea9d0 | 2019-10-31 11:40:30 +0100 | [diff] [blame] | 22 | "git.monogon.dev/source/nexantic.git/core/generated/api" |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 23 | "git.monogon.dev/source/nexantic.git/core/internal/common" |
Serge Bazanski | 7ba3152 | 2020-02-03 16:08:19 +0100 | [diff] [blame] | 24 | "google.golang.org/grpc/codes" |
| 25 | "google.golang.org/grpc/status" |
Lorenz Brun | a4ea9d0 | 2019-10-31 11:40:30 +0100 | [diff] [blame] | 26 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 27 | "errors" |
| 28 | |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 29 | "go.uber.org/zap" |
| 30 | ) |
| 31 | |
| 32 | var ( |
Serge Bazanski | 7ba3152 | 2020-02-03 16:08:19 +0100 | [diff] [blame] | 33 | ErrConsensusAlreadyProvisioned = status.Error(codes.FailedPrecondition, "consensus is already provisioned; make sure the data folder is empty") |
| 34 | ErrAlreadySetup = status.Error(codes.FailedPrecondition, "node is already set up") |
| 35 | ErrNotInJoinMode = status.Error(codes.FailedPrecondition, "node is not in the cluster join mode") |
| 36 | ErrTrustNotInitialized = status.Error(codes.FailedPrecondition, "trust backend not initialized") |
| 37 | ErrStorageNotInitialized = status.Error(codes.FailedPrecondition, "storage not initialized") |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 38 | ) |
| 39 | |
| 40 | func (s *SmalltownNode) CurrentState() common.SmalltownState { |
| 41 | return s.state |
| 42 | } |
| 43 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 44 | // InitializeNode contains functionality that needs to be executed regardless of what the node does |
| 45 | // later on |
| 46 | func (s *SmalltownNode) InitializeNode() (*api.NewNodeInfo, string, error) { |
| 47 | globalUnlockKey, err := s.Storage.InitializeData() |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 48 | if err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 49 | return nil, "", err |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 52 | nodeIP := s.Network.GetIP(true) |
Lorenz Brun | a4ea9d0 | 2019-10-31 11:40:30 +0100 | [diff] [blame] | 53 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 54 | nodeCert, nodeID, err := s.generateNodeID() |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 55 | if err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 56 | return nil, "", err |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 59 | return &api.NewNodeInfo{ |
| 60 | EnrolmentConfig: s.enrolmentConfig, |
| 61 | Ip: []byte(*nodeIP), |
| 62 | IdCert: nodeCert, |
| 63 | GlobalUnlockKey: globalUnlockKey, |
| 64 | }, nodeID, nil |
| 65 | } |
| 66 | |
| 67 | func (s *SmalltownNode) JoinCluster(context context.Context, req *api.JoinClusterRequest) (*api.JoinClusterResponse, error) { |
| 68 | if s.state != common.StateEnrollMode { |
| 69 | return nil, ErrNotInJoinMode |
| 70 | } |
| 71 | |
| 72 | s.logger.Info("Joining Consenus") |
| 73 | |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 74 | config := s.Consensus.GetConfig() |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 75 | config.Name = s.hostname |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 76 | config.InitialCluster = "default" // Clusters can't cross-join anyways due to cryptography |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 77 | s.Consensus.SetConfig(config) |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 78 | var err error |
| 79 | if err != nil { |
| 80 | s.logger.Warn("Invalid JoinCluster request", zap.Error(err)) |
| 81 | return nil, errors.New("invalid join request") |
| 82 | } |
| 83 | if err := s.Consensus.WriteCertificateFiles(req.Certs); err != nil { |
| 84 | return nil, err |
Lorenz Brun | a4ea9d0 | 2019-10-31 11:40:30 +0100 | [diff] [blame] | 85 | } |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 86 | |
| 87 | // Start consensus |
| 88 | err = s.Consensus.Start() |
| 89 | if err != nil { |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 90 | return nil, err |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 93 | s.state = common.StateJoined |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 94 | |
| 95 | s.logger.Info("Joined cluster. Node is now syncing.") |
| 96 | |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 97 | return &api.JoinClusterResponse{}, nil |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 98 | } |