-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathmapper.go
More file actions
124 lines (105 loc) · 3.24 KB
/
mapper.go
File metadata and controls
124 lines (105 loc) · 3.24 KB
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
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package config
import (
"log/slog"
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
)
// FromCommandProto maps the AgentConfig Command struct to the Command proto message
func FromCommandProto(config *mpi.CommandServer) *Command {
cmd := &Command{}
// Map ServerSettings to the ServerConfig
if config.GetServer() != nil && config.GetServer().GetHost() != "" && config.GetServer().GetPort() != 0 {
cmd.Server = &ServerConfig{
Host: config.GetServer().GetHost(),
Port: int(config.GetServer().GetPort()),
}
if config.GetServer().GetType() == mpi.ServerSettings_SERVER_SETTINGS_TYPE_GRPC {
cmd.Server.Type = Grpc
}
} else {
cmd.Server = nil
}
// Set Auth to be nil
cmd.Auth = nil
// Map TLSSettings to TLSConfig
if config.GetTls() != nil {
cmd.TLS = &TLSConfig{
Cert: config.GetTls().GetCert(),
Key: config.GetTls().GetKey(),
Ca: config.GetTls().GetCa(),
ServerName: config.GetTls().GetServerName(),
SkipVerify: config.GetTls().GetSkipVerify(),
}
if cmd.TLS.SkipVerify {
slog.Warn("Insecure setting SkipVerify, this tells the server to accept a certificate with any hostname.")
}
} else {
cmd.TLS = nil
}
return cmd
}
// ToCommandProto maps the AgentConfig Command struct back to the Command proto message
func ToCommandProto(cmd *Command) *mpi.CommandServer {
protoConfig := &mpi.CommandServer{}
// Map ServerConfig to the ServerSettings
if cmd.Server != nil {
protoServerType := mpi.ServerSettings_SERVER_SETTINGS_TYPE_UNDEFINED
if cmd.Server.Type == Grpc {
protoServerType = mpi.ServerSettings_SERVER_SETTINGS_TYPE_GRPC
}
protoConfig.Server = &mpi.ServerSettings{
Host: cmd.Server.Host,
Port: int32(cmd.Server.Port),
Type: protoServerType,
}
}
// Map AuthConfig to AuthSettings
if cmd.Auth != nil {
protoConfig.Auth = &mpi.AuthSettings{}
}
// Map TLSConfig to TLSSettings
if cmd.TLS != nil {
protoConfig.Tls = &mpi.TLSSettings{
Cert: cmd.TLS.Cert,
Key: cmd.TLS.Key,
Ca: cmd.TLS.Ca,
ServerName: cmd.TLS.ServerName,
SkipVerify: cmd.TLS.SkipVerify,
}
}
return protoConfig
}
// ToAuxiliaryCommandServerProto maps the AgentConfig Command struct back to the AuxiliaryCommandServer proto message
func ToAuxiliaryCommandServerProto(cmd *Command) *mpi.AuxiliaryCommandServer {
protoConfig := &mpi.AuxiliaryCommandServer{}
// Map ServerConfig to the ServerSettings
if cmd.Server != nil {
protoServerType := mpi.ServerSettings_SERVER_SETTINGS_TYPE_UNDEFINED
if cmd.Server.Type == Grpc {
protoServerType = mpi.ServerSettings_SERVER_SETTINGS_TYPE_GRPC
}
protoConfig.Server = &mpi.ServerSettings{
Host: cmd.Server.Host,
Port: int32(cmd.Server.Port),
Type: protoServerType,
}
}
// Map AuthConfig to AuthSettings
if cmd.Auth != nil {
protoConfig.Auth = &mpi.AuthSettings{}
}
// Map TLSConfig to TLSSettings
if cmd.TLS != nil {
protoConfig.Tls = &mpi.TLSSettings{
Cert: cmd.TLS.Cert,
Key: cmd.TLS.Key,
Ca: cmd.TLS.Ca,
ServerName: cmd.TLS.ServerName,
SkipVerify: cmd.TLS.SkipVerify,
}
}
return protoConfig
}