-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmetadata.go
More file actions
116 lines (99 loc) · 3.87 KB
/
Copy pathmetadata.go
File metadata and controls
116 lines (99 loc) · 3.87 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
// SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// 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 main
import (
"context"
"fmt"
"os"
"time"
"github.com/NVIDIA/fleet-intelligence-sdk/pkg/log"
pkgmetadata "github.com/NVIDIA/fleet-intelligence-sdk/pkg/metadata"
"github.com/NVIDIA/fleet-intelligence-sdk/pkg/sqlite"
"github.com/urfave/cli"
"github.com/NVIDIA/fleet-intelligence-agent/internal/cmdutil"
"github.com/NVIDIA/fleet-intelligence-agent/internal/config"
)
func metadataCommand(cliContext *cli.Context) error {
logLevel := cliContext.String("log-level")
zapLvl, err := log.ParseLogLevel(logLevel)
if err != nil {
return err
}
log.Logger = log.CreateLogger(zapLvl, "")
log.Logger.Debugw("starting metadata command")
rootCtx, rootCancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer rootCancel()
log.Logger.Debugw("getting state file")
stateFile, err := config.DefaultStateFile()
if err != nil {
return fmt.Errorf("failed to get state file: %w", err)
}
log.Logger.Debugw("successfully got state file")
// Check if we have read permission to the state file
if _, err := os.Open(stateFile); err != nil {
if os.IsPermission(err) {
return fmt.Errorf("insufficient permissions to read state file %s. Please run with sudo", stateFile)
}
// If it's not a permission error, continue - the sqlite.Open call below will handle other issues
}
log.Logger.Debugw("opening state file for reading")
dbRO, err := sqlite.Open(stateFile, sqlite.WithReadOnly(true))
if err != nil {
return fmt.Errorf("failed to open state file: %w", err)
}
defer dbRO.Close()
log.Logger.Debugw("successfully opened state file for reading")
metadata, err := pkgmetadata.ReadAllMetadata(rootCtx, dbRO)
if err != nil {
return fmt.Errorf("failed to read metadata: %w", err)
}
log.Logger.Debugw("successfully read metadata")
for k, v := range metadata {
// Mask sensitive tokens (JWT and SAK)
if k == pkgmetadata.MetadataKeyToken || k == "sak_token" {
v = pkgmetadata.MaskToken(v)
}
fmt.Printf("%s: %s\n", k, v)
}
setKey := cliContext.String("set-key")
setValue := cliContext.String("set-value")
if setKey == "" || setValue == "" { // no update/insert needed
return nil
}
// Check if we have write permission to the state file when setting metadata
if _, err := os.OpenFile(stateFile, os.O_WRONLY, 0); err != nil {
if os.IsPermission(err) {
return fmt.Errorf("insufficient permissions to write to state file %s. Please run with sudo", stateFile)
}
// If it's not a permission error, continue - the sqlite.Open call below will handle other issues
}
log.Logger.Debugw("opening state file for writing")
dbRW, err := sqlite.Open(stateFile)
if err != nil {
return fmt.Errorf("failed to open state file: %w", err)
}
defer dbRW.Close()
log.Logger.Debugw("successfully opened state file for writing")
log.Logger.Debugw("deleting metadata data")
if err := pkgmetadata.SetMetadata(rootCtx, dbRW, setKey, setValue); err != nil {
return fmt.Errorf("failed to update metadata: %w", err)
}
if err := config.SecureStateFilePermissions(stateFile); err != nil {
return fmt.Errorf("failed to secure state file permissions: %w", err)
}
log.Logger.Debugw("successfully updated metadata")
fmt.Printf("%s successfully updated metadata\n", cmdutil.CheckMark)
return nil
}