-
Notifications
You must be signed in to change notification settings - Fork 282
/
service.go
121 lines (102 loc) · 2.49 KB
/
service.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
// Copyright The Athenz Authors
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package svc
import (
"net"
"os"
"path/filepath"
"github.com/AthenZ/athenz/libs/go/athenz-common/log"
siafile "github.com/AthenZ/athenz/libs/go/sia/file"
)
type MsdHost struct {
HostDocument []byte
SiaConfig []byte
}
type Service struct {
Name string
KeyFilename string
CertFilename string
User string
Group string
Uid int
Gid int
FileMode int
}
type ServicesData struct {
SrvArr []Service
Ips []net.IP
Domain string
}
const (
PROFILE_CONFIG = "/etc/sia/profile_config"
SIA_CONFIG = "/etc/sia/sia_config"
SIA_DIR = "/var/lib/sia"
HOST_DOCUMENT = "host_document"
PROFILE_TAG_KEY = "profile:Tag"
)
var cloudFetcher Fetcher
var onPremFetcher Fetcher
var msdHost *MsdHost = nil
func SetCloudFetcher(fetcher Fetcher) {
cloudFetcher = fetcher
}
func SetOnPremFetcher(fetcher Fetcher) {
onPremFetcher = fetcher
}
// Map the service name to its service configuration
func GetServicesData(accountId string) (ServicesData, error) {
validateMsdHost()
// cloud host sia_config presented without host_document
if msdHost.SiaConfig != nil && msdHost.HostDocument == nil {
return cloudFetcher.Fetch(*msdHost, accountId)
}
// on-prem host
return onPremFetcher.Fetch(*msdHost, accountId)
}
func validateMsdHost() {
if msdHost == nil {
docBytes, docPath := ReadHostDocument()
siaBytes, siaPath := ReadSiaConfig()
msdHost = &MsdHost{
HostDocument: docBytes,
SiaConfig: siaBytes,
}
log.Debugf("HostDocument: %s", docPath)
log.Debugf("SiaConfig: %s", siaPath)
}
}
func GetAccountId() (string, error) {
validateMsdHost()
// cloud host sia_config presented without host_document
if msdHost.SiaConfig != nil && msdHost.HostDocument == nil {
return cloudFetcher.GetAccountId()
}
// on-prem host
return onPremFetcher.GetAccountId()
}
func ReadSiaConfig() ([]byte, string) {
if !siafile.Exists(SIA_DIR) {
log.Print("SIA_DIR not exist")
return nil, ""
}
if siafile.Exists(SIA_CONFIG) {
bytes, err := os.ReadFile(SIA_CONFIG)
if err != nil {
return nil, ""
} else if len(bytes) == 0 {
return nil, ""
}
return bytes, SIA_CONFIG
}
return nil, ""
}
func ReadHostDocument() ([]byte, string) {
docFile := filepath.Join(SIA_DIR, HOST_DOCUMENT)
if siafile.Exists(docFile) {
docBytes, err := os.ReadFile(docFile)
if err == nil {
return docBytes, docFile
}
}
return nil, ""
}