Skip to content

Commit 60c626b

Browse files
authored
Feat/ttls (#131)
* #672 add ttls conf to env * #672 add change requests * #672 add change requests * #672 add change requests * #672 change requests: return on err * #672 use intermediateCert for ttls conf
1 parent ff86dca commit 60c626b

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

Diff for: coordinator/core/marbleapi.go

+29
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"crypto/elliptic"
1414
"crypto/rand"
1515
"crypto/x509"
16+
"encoding/json"
17+
"encoding/pem"
1618
"math"
1719
"text/template"
1820
"time"
@@ -88,6 +90,12 @@ func (c *Core) Activate(ctx context.Context, req *rpc.ActivationReq) (*rpc.Activ
8890
}
8991

9092
marble := c.manifest.Marbles[req.GetMarbleType()] // existence has been checked in verifyManifestRequirement
93+
// add TTLS config to Env
94+
if err := c.setTTLSConfig(marble); err != nil {
95+
c.zaplogger.Error("Could not create TTLS config.", zap.Error(err))
96+
return nil, err
97+
}
98+
9199
params, err := customizeParameters(marble.Parameters, authSecrets, secrets)
92100
if err != nil {
93101
c.zaplogger.Error("Could not customize parameters.", zap.Error(err))
@@ -306,3 +314,24 @@ func (c *Core) generateMarbleAuthSecrets(req *rpc.ActivationReq, marbleUUID uuid
306314

307315
return authSecrets, nil
308316
}
317+
318+
func (c *Core) setTTLSConfig(marble manifest.Marble) error {
319+
ttlsConf := make(map[string]map[string]string)
320+
ttlsConf["tls"] = make(map[string]string)
321+
for _, tag := range marble.TLS {
322+
for _, entry := range c.manifest.TLS[tag].Outgoing {
323+
pemCert := pem.Block{Type: "CERTIFICATE", Bytes: c.intermediateCert.Raw}
324+
ttlsConf["tls"][entry.Addr+":"+entry.Port] = string(pem.EncodeToMemory(&pemCert))
325+
}
326+
}
327+
ttlsConfJSON, err := json.Marshal(ttlsConf)
328+
if err != nil {
329+
return err
330+
}
331+
if marble.Parameters.Env == nil {
332+
marble.Parameters.Env = make(map[string]string)
333+
}
334+
marble.Parameters.Env["MARBLE_TTLS_CONFIG"] = string(ttlsConfJSON)
335+
336+
return nil
337+
}

Diff for: coordinator/core/marbleapi_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ func (ms *marbleSpawner) newMarble(marbleType string, infraName string, shouldSu
245245
}
246246
ms.mutex.Unlock()
247247
}
248+
249+
// Validate ttls conf
250+
config := make(map[string]map[string]string)
251+
ms.assert.NoError(json.Unmarshal([]byte(params.Env["MARBLE_TTLS_CONFIG"]), &config))
252+
if marbleType == "backend_first" {
253+
ms.assert.NotEqual(nil, config["tls"]["localhost:8080"])
254+
ms.assert.NotEqual(nil, config["tls"]["service.namespace:4242"])
255+
} else if marbleType == "backend_other" {
256+
ms.assert.NotEqual(nil, config["tls"]["localhost:8080"])
257+
ms.assert.NotEqual(nil, config["tls"]["service.namespace:4242"])
258+
ms.assert.NotEqual(nil, config["tls"]["example.com:40000"])
259+
} else if marbleType == "frontend" {
260+
ms.assert.NotEqual(nil, config["tls"])
261+
ms.assert.Equal(0, len(config["tls"]))
262+
}
248263
}
249264

250265
func (ms *marbleSpawner) newMarbleAsync(marbleType string, infraName string, shouldSucceed bool) {

Diff for: coordinator/manifest/manifest.go

+18
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Manifest struct {
3838
Secrets map[string]Secret
3939
// RecoveryKeys holds one or multiple RSA public keys to encrypt multiple secrets, which can be used to decrypt the sealed state again in case the encryption key on disk was corrupted somehow.
4040
RecoveryKeys map[string]string
41+
// TLS contains tags which can be assiged to Marbles to specify which connections should be elevated to TLS
42+
TLS map[string]TLStag
4143
}
4244

4345
// Marble describes a service in the mesh that should be handled and verified by the Coordinator
@@ -49,6 +51,22 @@ type Marble struct {
4951
// Parameters contains lists for files, environment variables and commandline arguments that should be passed to the application.
5052
// Placeholder variables are supported for specific assets of the marble's activation process.
5153
Parameters *rpc.Parameters
54+
// TLS holds a list of tags which are specified in the manifest
55+
TLS []string
56+
}
57+
58+
// TLStag describes which entries should be used to determine the ttls connections of a marble
59+
type TLStag struct {
60+
// Outgoing holds a list of all outgoing addresses that should be elevated to TLS
61+
Outgoing []TLSTagEntry
62+
// Incoming holds a list of all incoming addresses that should be elevated to TLS
63+
Incoming []TLSTagEntry
64+
}
65+
66+
// TLSTagEntry describes one connection which should be elevated to ttls
67+
type TLSTagEntry struct {
68+
Port string
69+
Addr string
5270
}
5371

5472
// Check checks if the manifest is consistent.

Diff for: test/manifests.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ const ManifestJSON string = `{
6969
"--first",
7070
"serve"
7171
]
72-
}
72+
},
73+
"TLS": [
74+
"web"
75+
]
7376
},
7477
"backend_other": {
7578
"Package": "backend",
@@ -82,7 +85,10 @@ const ManifestJSON string = `{
8285
"Argv": [
8386
"serve"
8487
]
85-
}
88+
},
89+
"TLS": [
90+
"web", "anotherWeb"
91+
]
8692
},
8793
"frontend": {
8894
"Package": "frontend",
@@ -130,6 +136,28 @@ const ManifestJSON string = `{
130136
},
131137
"ValidFor": 7
132138
}
139+
},
140+
"TLS": {
141+
"web": {
142+
"Outgoing": [
143+
{
144+
"Port": "8080",
145+
"Addr": "localhost"
146+
},
147+
{
148+
"Port": "4242",
149+
"Addr": "service.namespace"
150+
}
151+
]
152+
},
153+
"anotherWeb": {
154+
"Outgoing": [
155+
{
156+
"Port": "40000",
157+
"Addr": "example.com"
158+
}
159+
]
160+
}
133161
}
134162
}`
135163

0 commit comments

Comments
 (0)