blob: 23eb24cba79905cb10b4e5d2eb423526b02ab022 [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"
21
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010022 "git.monogon.dev/source/nexantic.git/core/generated/api"
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020023 "git.monogon.dev/source/nexantic.git/core/internal/common"
Serge Bazanski7ba31522020-02-03 16:08:19 +010024 "google.golang.org/grpc/codes"
25 "google.golang.org/grpc/status"
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010026
Lorenz Brunaa6b7342019-12-12 02:55:02 +010027 "errors"
28
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020029 "go.uber.org/zap"
30)
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
46func (s *SmalltownNode) InitializeNode() (*api.NewNodeInfo, string, error) {
47 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
Lorenz Brunaa6b7342019-12-12 02:55:02 +010052 nodeIP := s.Network.GetIP(true)
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010053
Lorenz Brunaa6b7342019-12-12 02:55:02 +010054 nodeCert, nodeID, err := s.generateNodeID()
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020055 if err != nil {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010056 return nil, "", err
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020057 }
58
Lorenz Brunaa6b7342019-12-12 02:55:02 +010059 return &api.NewNodeInfo{
60 EnrolmentConfig: s.enrolmentConfig,
61 Ip: []byte(*nodeIP),
62 IdCert: nodeCert,
63 GlobalUnlockKey: globalUnlockKey,
64 }, nodeID, nil
65}
66
67func (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 Hofstadt0d7c91e2019-10-23 21:44:47 +020074 config := s.Consensus.GetConfig()
Leopold Schabel68c58752019-11-14 21:00:59 +010075 config.Name = s.hostname
Lorenz Brunaa6b7342019-12-12 02:55:02 +010076 config.InitialCluster = "default" // Clusters can't cross-join anyways due to cryptography
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020077 s.Consensus.SetConfig(config)
Lorenz Brunaa6b7342019-12-12 02:55:02 +010078 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 Bruna4ea9d02019-10-31 11:40:30 +010085 }
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020086
87 // Start consensus
88 err = s.Consensus.Start()
89 if err != nil {
Lorenz Brunaa6b7342019-12-12 02:55:02 +010090 return nil, err
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020091 }
92
Lorenz Brunaa6b7342019-12-12 02:55:02 +010093 s.state = common.StateJoined
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020094
95 s.logger.Info("Joined cluster. Node is now syncing.")
96
Lorenz Brunaa6b7342019-12-12 02:55:02 +010097 return &api.JoinClusterResponse{}, nil
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020098}