blob: cbbfd4d940a336ff1891b95c016ae125c6333d19 [file] [log] [blame]
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +02001// 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
17package node
18
19import (
Lorenz Brunaa6b7342019-12-12 02:55:02 +010020 "context"
Serge Bazanskicdb8c782020-02-17 12:34:02 +010021 "errors"
22 "fmt"
Lorenz Brunaa6b7342019-12-12 02:55:02 +010023
Serge Bazanskicdb8c782020-02-17 12:34:02 +010024 "go.uber.org/zap"
Serge Bazanski7ba31522020-02-03 16:08:19 +010025 "google.golang.org/grpc/codes"
26 "google.golang.org/grpc/status"
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010027
Serge Bazanskicdb8c782020-02-17 12:34:02 +010028 "git.monogon.dev/source/nexantic.git/core/generated/api"
29 "git.monogon.dev/source/nexantic.git/core/internal/common"
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020030)
31
32var (
Serge Bazanski7ba31522020-02-03 16:08:19 +010033 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 Hofstadt0d7c91e2019-10-23 21:44:47 +020038)
39
40func (s *SmalltownNode) CurrentState() common.SmalltownState {
41 return s.state
42}
43
Lorenz Brunaa6b7342019-12-12 02:55:02 +010044// InitializeNode contains functionality that needs to be executed regardless of what the node does
45// later on
Serge Bazanskicdb8c782020-02-17 12:34:02 +010046func (s *SmalltownNode) InitializeNode(ctx context.Context) (*api.NewNodeInfo, string, error) {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010047 globalUnlockKey, err := s.Storage.InitializeData()
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020048 if err != nil {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010049 return nil, "", err
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020050 }
51
Serge Bazanskicdb8c782020-02-17 12:34:02 +010052 nodeIP, err := s.Network.GetIP(ctx, true)
53 if err != nil {
54 return nil, "", fmt.Errorf("could not get IP: %v", err)
55 }
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010056
Lorenz Brunaa6b7342019-12-12 02:55:02 +010057 nodeCert, nodeID, err := s.generateNodeID()
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020058 if err != nil {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010059 return nil, "", err
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020060 }
61
Lorenz Brunaa6b7342019-12-12 02:55:02 +010062 return &api.NewNodeInfo{
63 EnrolmentConfig: s.enrolmentConfig,
64 Ip: []byte(*nodeIP),
65 IdCert: nodeCert,
66 GlobalUnlockKey: globalUnlockKey,
67 }, nodeID, nil
68}
69
70func (s *SmalltownNode) JoinCluster(context context.Context, req *api.JoinClusterRequest) (*api.JoinClusterResponse, error) {
71 if s.state != common.StateEnrollMode {
72 return nil, ErrNotInJoinMode
73 }
74
75 s.logger.Info("Joining Consenus")
76
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020077 config := s.Consensus.GetConfig()
Leopold Schabel68c58752019-11-14 21:00:59 +010078 config.Name = s.hostname
Lorenz Brunaa6b7342019-12-12 02:55:02 +010079 config.InitialCluster = "default" // Clusters can't cross-join anyways due to cryptography
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020080 s.Consensus.SetConfig(config)
Lorenz Brunaa6b7342019-12-12 02:55:02 +010081 var err error
82 if err != nil {
83 s.logger.Warn("Invalid JoinCluster request", zap.Error(err))
84 return nil, errors.New("invalid join request")
85 }
86 if err := s.Consensus.WriteCertificateFiles(req.Certs); err != nil {
87 return nil, err
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010088 }
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020089
90 // Start consensus
91 err = s.Consensus.Start()
92 if err != nil {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010093 return nil, err
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020094 }
95
Lorenz Brunaa6b7342019-12-12 02:55:02 +010096 s.state = common.StateJoined
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020097
98 s.logger.Info("Joined cluster. Node is now syncing.")
99
Lorenz Brunaa6b7342019-12-12 02:55:02 +0100100 return &api.JoinClusterResponse{}, nil
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +0200101}