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 common |
| 18 | |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame] | 19 | import ( |
| 20 | "crypto/ed25519" |
| 21 | "encoding/hex" |
| 22 | "errors" |
| 23 | "strings" |
| 24 | ) |
| 25 | |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 26 | type ( |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 27 | SmalltownState string |
| 28 | ) |
| 29 | |
| 30 | const ( |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 31 | // These are here to prevent depdendency loops |
| 32 | NodeServicePort = 7835 |
| 33 | ConsensusPort = 7834 |
| 34 | MasterServicePort = 7833 |
| 35 | ExternalServicePort = 7836 |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 36 | DebugServicePort = 7837 |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 37 | KubernetesAPIPort = 6443 |
Lorenz Brun | 70f65b2 | 2020-07-08 17:02:47 +0200 | [diff] [blame^] | 38 | DebuggerPort = 2345 |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | const ( |
| 42 | // Node is provisioning a new cluster with itself as a master |
| 43 | StateNewClusterMode SmalltownState = "setup" |
| 44 | // Node is enrolling itself and waiting to be adopted |
| 45 | StateEnrollMode SmalltownState = "join" |
Leopold Schabel | 68c5875 | 2019-11-14 21:00:59 +0100 | [diff] [blame] | 46 | // Node is fully provisioned. |
Lorenz Brun | aa6b734 | 2019-12-12 02:55:02 +0100 | [diff] [blame] | 47 | StateJoined SmalltownState = "enrolled" |
Hendrik Hofstadt | 0d7c91e | 2019-10-23 21:44:47 +0200 | [diff] [blame] | 48 | ) |
Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame] | 49 | |
| 50 | func NameFromIDKey(pubKey ed25519.PublicKey) string { |
| 51 | return "smalltown-" + hex.EncodeToString(pubKey[:16]) |
| 52 | } |
| 53 | |
| 54 | func IDKeyPrefixFromName(name string) ([]byte, error) { |
| 55 | if !strings.HasPrefix(name, "smalltown-") { |
| 56 | return []byte{}, errors.New("invalid name") |
| 57 | } |
| 58 | return hex.DecodeString(strings.TrimPrefix(name, "smalltown-")) |
| 59 | } |