-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrpc_server.go
More file actions
241 lines (204 loc) · 9.02 KB
/
grpc_server.go
File metadata and controls
241 lines (204 loc) · 9.02 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// SPDX-FileCopyrightText: (C) 2025 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
// Package hostmgr implements the Host Manager gRPC service.
package hostmgr
import (
"context"
"google.golang.org/grpc/codes"
computev1 "github.com/open-edge-platform/infra-core/inventory/v2/pkg/api/compute/v1"
inv_errors "github.com/open-edge-platform/infra-core/inventory/v2/pkg/errors"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/policy/rbac"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/tenant"
"github.com/open-edge-platform/infra-managers/host/pkg/alivemgr"
pb "github.com/open-edge-platform/infra-managers/host/pkg/api/hostmgr/proto"
"github.com/open-edge-platform/infra-managers/host/pkg/errors"
inv_mgr_cli "github.com/open-edge-platform/infra-managers/host/pkg/invclient"
hmgr_util "github.com/open-edge-platform/infra-managers/host/pkg/utils"
)
type server struct {
pb.UnimplementedHostmgrServer
rbac *rbac.Policy
authEnabled bool
}
//nolint:stylecheck,revive // name of this function should be aligned with the one in .pb.go
func (s *server) UpdateHostStatusByHostGuid(ctx context.Context,
in *pb.UpdateHostStatusByHostGuidRequest,
) (*pb.HostStatusResp, error) {
zlog.Info().Msgf("Updating Host (%s) status", in.GetHostGuid())
if s.authEnabled {
if !s.rbac.IsRequestAuthorized(ctx, rbac.UpdateKey) {
err := inv_errors.Errorfc(codes.PermissionDenied, "Request is blocked by RBAC")
zlog.InfraSec().InfraErr(err).Msgf("Request UpdateHostStatusByHostGuid is not authenticated")
return nil, err
}
}
if err := in.ValidateAll(); err != nil {
zlog.InfraSec().InfraErr(err).Msgf("Invalid request: %v", in)
return nil, errors.Wrap(err)
}
guid := in.GetHostGuid()
status := in.GetHostStatus()
tenantID, present := tenant.GetTenantIDFromContext(ctx)
if !present {
// This should never happen! Interceptor should either fail or set it!
err := inv_errors.Errorfc(codes.Unauthenticated, "Tenant ID is not present in context")
zlog.InfraSec().InfraErr(err).Msg("Request UpdateHostStatusByHostGuid is not authenticated")
return nil, err
}
hostResc, err := inv_mgr_cli.GetHostResourceByGUID(ctx, invClientInstance, tenantID, guid)
if err != nil {
return nil, inv_errors.ErrorToSanitizedGrpcError(err)
}
if hmgr_util.IsHostUntrusted(hostResc) {
zlog.InfraSec().InfraError("Host tID=%s, UUID=%s is not trusted, the message will not be handled", tenantID, guid).
Msg("UpdateHostStatusByHostGuid")
return nil, inv_errors.Errorfc(codes.Unauthenticated,
"Host tID=%s, UUID=%s is not trusted, the message will not be handled", tenantID, guid)
}
err = s.updateHostStatusIfNeeded(ctx, tenantID, hostResc, status)
if err != nil {
return nil, err
}
return &pb.HostStatusResp{}, nil
}
//nolint:cyclop // cyclomatic complexity is high due to update of various Host components
func (s *server) UpdateHostSystemInfoByGUID(ctx context.Context,
in *pb.UpdateHostSystemInfoByGUIDRequest,
) (*pb.UpdateHostSystemInfoByGUIDResponse, error) {
zlog.Info().Msgf("Updating Host (%s) system information", in.GetHostGuid())
if s.authEnabled {
if !s.rbac.IsRequestAuthorized(ctx, rbac.UpdateKey) {
err := inv_errors.Errorfc(codes.PermissionDenied, "Request is blocked by RBAC")
zlog.InfraSec().InfraErr(err).Msgf("Request UpdateHostSystemInfoByGUID is not authenticated")
return nil, err
}
}
if err := in.ValidateAll(); err != nil {
zlog.InfraSec().InfraErr(err).Msgf("Invalid request %v", in)
return nil, errors.Wrap(err)
}
guid := in.GetHostGuid()
systemInfo := in.GetSystemInfo()
tenantID, present := tenant.GetTenantIDFromContext(ctx)
if !present {
// This should never happen! Interceptor should either fail or set it!
err := inv_errors.Errorfc(codes.Unauthenticated, "Tenant ID is not present in context")
zlog.InfraSec().InfraErr(err).Msg("Request UpdateHostSystemInfoByGUID is not authenticated")
return nil, err
}
hostres, err := inv_mgr_cli.GetHostResourceByGUID(ctx, invClientInstance, tenantID, guid)
if err != nil {
return nil, inv_errors.ErrorToSanitizedGrpcError(err)
}
if hmgr_util.IsHostUntrusted(hostres) {
zlog.InfraSec().InfraError("Host tID=%s, UUID=%s is not trusted, the message will not be handled", tenantID, guid).
Msg("UpdateHostSystemInfoByGUID")
return nil, inv_errors.Errorfc(codes.Unauthenticated,
"Host tID=%s, UUID=%s is not trusted, the message will not be handled", tenantID, guid)
}
if !DisabledProvisioningValue && hmgr_util.IsHostNotProvisioned(hostres) {
zlog.InfraSec().
InfraError("Host tID=%s, UUID=%s is not yet provisioned, skipping update", tenantID, hostres.GetUuid()).
Msg("UpdateHostSystemInfoByGUID")
return nil, inv_errors.Errorfc(codes.FailedPrecondition, "")
}
err = updateHost(ctx, tenantID, hostres, systemInfo)
if err != nil {
return nil, inv_errors.ErrorToSanitizedGrpcError(err)
}
if err := updateHoststorage(ctx, tenantID, hostres, systemInfo.HwInfo); err != nil {
return nil, inv_errors.ErrorToSanitizedGrpcError(err)
}
if err := updateHostnics(ctx, tenantID, hostres, systemInfo.HwInfo); err != nil {
return nil, inv_errors.ErrorToSanitizedGrpcError(err)
}
if err := updateHostusbs(ctx, tenantID, hostres, systemInfo.HwInfo); err != nil {
return nil, inv_errors.ErrorToSanitizedGrpcError(err)
}
if err := updateHostgpus(ctx, tenantID, hostres, systemInfo.HwInfo); err != nil {
return nil, inv_errors.ErrorToSanitizedGrpcError(err)
}
return &pb.UpdateHostSystemInfoByGUIDResponse{}, nil
}
func (s *server) UpdateInstanceStateStatusByHostGUID(
ctx context.Context,
in *pb.UpdateInstanceStateStatusByHostGUIDRequest,
) (*pb.UpdateInstanceStateStatusByHostGUIDResponse, error) {
if s.authEnabled {
if !s.rbac.IsRequestAuthorized(ctx, rbac.UpdateKey) {
err := inv_errors.Errorfc(codes.PermissionDenied, "Request is blocked by RBAC")
zlog.InfraSec().InfraErr(err).Msgf("Request UpdateInstanceStateStatusByHostGUID is not authenticated")
return nil, err
}
}
if err := in.ValidateAll(); err != nil {
zlog.InfraSec().InfraErr(err).Msgf("Invalid request %v", in)
return nil, errors.Wrap(err)
}
tenantID, present := tenant.GetTenantIDFromContext(ctx)
if !present {
// This should never happen! Interceptor should either fail or set it!
err := inv_errors.Errorfc(codes.Unauthenticated, "Tenant ID is not present in context")
zlog.InfraSec().InfraErr(err).Msg("Request UpdateInstanceStateStatusByHostGUID is not authenticated")
return nil, err
}
zlog.Info().Msgf("Updating an Instance for Host (tID=%s, UUID=%s)", tenantID, in.GetHostGuid())
// Finding a Host by GUID to get its ResourceID first (needed for querying Instance)
host, err := inv_mgr_cli.GetHostResourceByGUID(ctx, invClientInstance, tenantID, in.GetHostGuid())
if err != nil {
return &pb.UpdateInstanceStateStatusByHostGUIDResponse{}, inv_errors.ErrorToSanitizedGrpcError(err)
}
if hmgr_util.IsHostUntrusted(host) {
zlog.InfraSec().
InfraError("Skip updating instance state for host tID=%s, UUID=%s (untrusted).", tenantID, host.GetUuid()).
Msg("UpdateInstanceStateStatusByHostGUID")
return &pb.UpdateInstanceStateStatusByHostGUIDResponse{}, inv_errors.Errorfc(codes.Unauthenticated, "")
}
// Deal first with Host status update
err = s.updateHostStatusIfNeeded(ctx, tenantID, host, hmgr_util.InstanceStatusToHostStatusMsg(in))
if err != nil {
return nil, err
}
if !DisabledProvisioningValue {
err = updateInstanceStateStatusByHostGUID(ctx, tenantID, host.GetInstance(), in)
if err != nil {
return nil, inv_errors.ErrorToSanitizedGrpcError(err)
}
}
return &pb.UpdateInstanceStateStatusByHostGUIDResponse{}, nil
}
func (s *server) updateHostStatusIfNeeded(
ctx context.Context, tenantID string, host *computev1.HostResource, status *pb.HostStatus,
) error {
hostUUID := host.GetUuid()
// update host heartbeat
if err := alivemgr.UpdateHostHeartBeat(host); err != nil {
zlog.Warn().Err(err).Msg("Failed to update host heartbeat")
}
// If host under maintenance, skip everything else
if hmgr_util.IsHostUnderMaintain(host) {
zlog.Info().Msgf("Skip host status update for host tID=%s, UUID=%s is under maintain.", tenantID, hostUUID)
return nil
}
if !DisabledProvisioningValue && hmgr_util.IsHostNotProvisioned(host) {
zlog.InfraSec().
InfraError("Skip updating instance state for host tID=%s, UUID=%s (not provisioned)", tenantID, host.GetUuid()).
Msg("UpdateInstanceStateStatusByHostGUID")
return inv_errors.Errorfc(codes.FailedPrecondition, "")
}
if hmgr_util.IsSameHostStatus(host, status) {
zlog.Info().Msgf("Skip host tID=%s, UUID=%s status update for no changes.", tenantID, hostUUID)
return nil
}
hostStatusName := pb.HostStatus_HostStatus_name[int32(status.GetHostStatus())]
zlog.Debug().Msgf("Update host resc (tID=%s, resID=%v) status: %v", tenantID, host.GetResourceId(),
hostStatusName)
if err := inv_mgr_cli.SetHostStatus(ctx, invClientInstance, tenantID, host.GetResourceId(),
hmgr_util.GetHostStatus(status.GetHostStatus()),
); err != nil {
zlog.InfraSec().InfraError("Failed to update host resource info").Msg("updateHostStatusIfNeeded")
return inv_errors.ErrorToSanitizedGrpcError(err)
}
return nil
}