-
Notifications
You must be signed in to change notification settings - Fork 161
/
drkey.go
174 lines (155 loc) · 5.22 KB
/
drkey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2022 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"io"
"net/netip"
"strings"
"time"
"github.com/scionproto/scion/pkg/drkey"
"github.com/scionproto/scion/pkg/private/serrors"
"github.com/scionproto/scion/private/config"
"github.com/scionproto/scion/private/storage"
)
const (
// DefaultEpochDuration is the default duration for the drkey SecretValue and derived keys
DefaultEpochDuration = 24 * time.Hour
DefaultPrefetchEntries = 10000
EnvVarEpochDuration = "SCION_TESTING_DRKEY_EPOCH_DURATION"
// DefaultAcceptanceWindowOffset is the time width for accepting incoming packets. The
// acceptance widown is then compute as:
// aw := [T-a, T+a)
// where aw:= acceptance window, T := time instant and a := acceptanceWindowOffset
//
// Picking the value equal or shorter than half of the drkey Grace Period ensures
// that we accept packets for active keys only.
DefaultAcceptanceWindowOffset = 2*time.Second + 500*time.Millisecond
EnvVarAccpetanceWindow = "SCION_TESTING_ACCEPTANCE_WINDOW"
)
var _ (config.Config) = (*DRKeyConfig)(nil)
// DRKeyConfig is the configuration for the connection to the trust database.
type DRKeyConfig struct {
Level1DB storage.DBConfig `toml:"level1_db,omitempty"`
SecretValueDB storage.DBConfig `toml:"secret_value_db,omitempty"`
Delegation SecretValueHostList `toml:"delegation,omitempty"`
PrefetchEntries int `toml:"prefetch_entries,omitempty"`
}
// InitDefaults initializes values of unset keys and determines if the configuration enables DRKey.
func (cfg *DRKeyConfig) InitDefaults() {
if cfg.PrefetchEntries == 0 {
cfg.PrefetchEntries = DefaultPrefetchEntries
}
config.InitAll(
cfg.Level1DB.WithDefault(""),
cfg.SecretValueDB.WithDefault(""),
&cfg.Delegation,
)
}
// Enabled returns true if DRKey is configured. False otherwise.
func (cfg *DRKeyConfig) Enabled() bool {
return cfg.Level1DB.Connection != ""
}
// Validate validates that all values are parsable.
func (cfg *DRKeyConfig) Validate() error {
return config.ValidateAll(&cfg.Level1DB, &cfg.SecretValueDB, &cfg.Delegation)
}
// Sample writes a config sample to the writer.
func (cfg *DRKeyConfig) Sample(dst io.Writer, path config.Path, ctx config.CtxMap) {
config.WriteString(dst, drkeySample)
config.WriteSample(dst, path,
config.CtxMap{config.ID: idSample},
config.OverrideName(
config.FormatData(
&cfg.Level1DB,
storage.SetID(storage.SampleDRKeyLevel1DB, idSample).Connection,
),
"level1_db",
),
config.OverrideName(
config.FormatData(
&cfg.SecretValueDB,
storage.SetID(storage.SampleDRKeySecretValueDB, idSample).Connection,
),
"secret_value_db",
),
&cfg.Delegation,
)
}
// ConfigName is the key in the toml file.
func (cfg *DRKeyConfig) ConfigName() string {
return "drkey"
}
// SecretValueHostList configures which endhosts can get delegation secrets, per protocol.
type SecretValueHostList map[string][]string
var _ (config.Config) = (*SecretValueHostList)(nil)
// InitDefaults will not add or modify any entry in the config.
func (cfg *SecretValueHostList) InitDefaults() {
if *cfg == nil {
*cfg = make(SecretValueHostList)
}
}
// Validate validates that the protocols exist, and their addresses are parsable.
func (cfg *SecretValueHostList) Validate() error {
for proto, list := range *cfg {
protoString := "PROTOCOL_" + strings.ToUpper(proto)
protoID, ok := drkey.ProtocolStringToId(protoString)
if !ok {
return serrors.New("Configured protocol not found", "protocol", proto)
}
if protoID == drkey.Generic {
return serrors.New("GENERIC protocol is not allowed")
}
for _, ip := range list {
if _, err := netip.ParseAddr(ip); err != nil {
return serrors.New("Syntax error: not a valid address", "ip", ip)
}
}
}
return nil
}
// Sample writes a config sample to the writer.
func (cfg *SecretValueHostList) Sample(dst io.Writer, path config.Path, ctx config.CtxMap) {
config.WriteString(dst, drkeySecretValueHostListSample)
}
// ConfigName is the key in the toml file.
func (cfg *SecretValueHostList) ConfigName() string {
return "delegation"
}
type HostProto struct {
Host netip.Addr
Proto drkey.Protocol
}
// ToAllowedSet will return map where there is a set of supported (Host,Protocol).
func (cfg *SecretValueHostList) ToAllowedSet() map[HostProto]struct{} {
m := make(map[HostProto]struct{})
for proto, ipList := range *cfg {
for _, ip := range ipList {
host, err := netip.ParseAddr(ip)
if err != nil {
continue
}
protoString := "PROTOCOL_" + strings.ToUpper(proto)
protoID, ok := drkey.ProtocolStringToId(protoString)
if !ok {
continue
}
hostProto := HostProto{
Host: host,
Proto: protoID,
}
m[hostProto] = struct{}{}
}
}
return m
}