Skip to content

Commit e04417f

Browse files
committed
Display JSON config in admin
1 parent 3be2ada commit e04417f

File tree

9 files changed

+237
-49
lines changed

9 files changed

+237
-49
lines changed

cmd/admin/handlers-get.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,11 @@ func settingsGETHandler(w http.ResponseWriter, r *http.Request) {
705705
}
706706
// Get context data
707707
ctx := r.Context().Value(contextKey("session")).(contextValue)
708+
// Get JSON values
709+
svcJSON, err := settingsmgr.RetrieveAllJSON(serviceVar)
710+
if err != nil {
711+
log.Printf("error getting JSON values: %v", err)
712+
}
708713
// Prepare template data
709714
templateData := SettingsTemplateData{
710715
Title: "Manage settings",
@@ -714,6 +719,7 @@ func settingsGETHandler(w http.ResponseWriter, r *http.Request) {
714719
Environments: envAll,
715720
Platforms: platforms,
716721
CurrentSettings: _settings,
722+
ServiceConfig: toJSONConfigurationService(svcJSON),
717723
TLSDebug: settingsmgr.DebugService(settings.ServiceTLS),
718724
AdminDebug: settingsmgr.DebugService(settings.ServiceAdmin),
719725
AdminDebugHTTP: settingsmgr.DebugHTTP(settings.ServiceAdmin),

cmd/admin/handlers-post.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ func settingsPOSTHandler(w http.ResponseWriter, r *http.Request) {
899899
case settings.TypeInteger:
900900
err = settingsmgr.SetInteger(stringToInteger(s.Value), serviceVar, s.Name)
901901
case settings.TypeString:
902-
err = settingsmgr.SetString(s.Value, serviceVar, s.Name)
902+
err = settingsmgr.SetString(s.Value, serviceVar, s.Name, false)
903903
}
904904
if err != nil {
905905
responseMessage = "error changing setting"

cmd/admin/main.go

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const (
4040
// Default endpoint to handle HTTP errors
4141
errorPath string = "/error"
4242
// Service configuration file
43-
configurationFile string = "config/admin.json"
43+
configurationFile string = "config/" + settings.ServiceAdmin + ".json"
4444
// osquery version to display tables
4545
osqueryTablesVersion string = "3.3.2"
4646
// JSON file with osquery tables data
@@ -70,38 +70,42 @@ var (
7070
osqueryTables []OsqueryTable
7171
)
7272

73-
// Function to load the configuration file and assign to variables
74-
func loadConfiguration() error {
75-
log.Printf("Loading %s", configurationFile)
73+
// Function to load the configuration file
74+
func loadConfiguration(file, service string) (types.JSONConfigurationService, error) {
75+
var cfg types.JSONConfigurationService
76+
log.Printf("Loading %s", file)
7677
// Load file and read config
77-
viper.SetConfigFile(configurationFile)
78+
viper.SetConfigFile(file)
7879
err := viper.ReadInConfig()
7980
if err != nil {
80-
return err
81+
return cfg, err
8182
}
8283
// TLS Admin values
83-
adminRaw := viper.Sub("admin")
84-
err = adminRaw.Unmarshal(&adminConfig)
84+
adminRaw := viper.Sub(service)
85+
err = adminRaw.Unmarshal(&cfg)
8586
if err != nil {
86-
return err
87+
return cfg, err
8788
}
8889
// Load configuration for the auth method
89-
if adminConfig.Auth == settings.AuthSAML {
90-
samlRaw := viper.Sub(settings.AuthSAML)
91-
err = samlRaw.Unmarshal(&samlConfig)
92-
if err != nil {
93-
return err
90+
/*
91+
if adminConfig.Auth == settings.AuthSAML {
92+
samlRaw := viper.Sub(settings.AuthSAML)
93+
err = samlRaw.Unmarshal(&samlConfig)
94+
if err != nil {
95+
return cfg, err
96+
}
9497
}
95-
}
98+
*/
9699
// No errors!
97-
return nil
100+
return cfg, nil
98101
}
99102

100103
// Function to load the JSON data for osquery tables
101-
func loadOsqueryTables() error {
102-
jsonFile, err := os.Open(osqueryTablesFile)
104+
func loadOsqueryTables(file string) ([]OsqueryTable, error) {
105+
var tables []OsqueryTable
106+
jsonFile, err := os.Open(file)
103107
if err != nil {
104-
return err
108+
return tables, err
105109
}
106110
//defer jsonFile.Close()
107111
defer func() {
@@ -111,31 +115,34 @@ func loadOsqueryTables() error {
111115
}
112116
}()
113117
byteValue, _ := ioutil.ReadAll(jsonFile)
114-
err = json.Unmarshal(byteValue, &osqueryTables)
118+
err = json.Unmarshal(byteValue, &tables)
115119
if err != nil {
116-
return err
120+
return tables, err
117121
}
118122
// Add a string for platforms to be used as filter
119-
for i, t := range osqueryTables {
123+
for i, t := range tables {
120124
filter := ""
121125
for _, p := range t.Platforms {
122126
filter += " filter-" + p
123127
}
124-
osqueryTables[i].Filter = strings.TrimSpace(filter)
128+
tables[i].Filter = strings.TrimSpace(filter)
125129
}
126-
return nil
130+
return tables, nil
127131
}
128132

129133
// Initialization code
130134
func init() {
135+
var err error
131136
// Logging flags
132137
log.SetFlags(log.Lshortfile)
133-
// Load configuration
134-
if err := loadConfiguration(); err != nil {
135-
log.Fatalf("Error loading configuration %s", err)
138+
// Load admin configuration
139+
adminConfig, err = loadConfiguration(configurationFile, settings.ServiceAdmin)
140+
if err != nil {
141+
log.Fatalf("Error loading %s - %s", configurationFile, err)
136142
}
137143
// Load osquery tables JSON
138-
if err := loadOsqueryTables(); err != nil {
144+
osqueryTables, err = loadOsqueryTables(osqueryTablesFile)
145+
if err != nil {
139146
log.Fatalf("Error loading osquery tables %s", err)
140147
}
141148
}
@@ -210,6 +217,10 @@ func main() {
210217
log.Fatalf("Failed to add %s to configuration: %v", settings.InactiveHours, err)
211218
}
212219
}
220+
// Write JSON config to settings
221+
if err := settingsmgr.SetAllJSON(settings.ServiceAdmin, adminConfig.Listener, adminConfig.Port, adminConfig.Host, adminConfig.Auth, adminConfig.Logging); err != nil {
222+
log.Fatalf("Failed to add JSON values to configuration: %v", err)
223+
}
213224
// multiple listeners channel
214225
finish := make(chan bool)
215226
// Start SAML Middleware if we are using SAML

cmd/admin/templates/settings.html

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
<div class="card mt-2">
22+
2223
<!--
2324
<div class="warning-back stripes-warning text-center">
2425
<h5 class="warning-text">
@@ -28,7 +29,7 @@ <h5 class="warning-text">
2829
-->
2930

3031
<div class="card-header">
31-
<i class="fas fa-tools"></i> Settings for <b>{{ .Service }}</b> Service</b>
32+
<i class="fas fa-tools"></i> Settings for <b>{{ .Service }}</b> Service
3233

3334
<div class="card-header-actions">
3435
<div class="row">
@@ -87,6 +88,43 @@ <h5 class="warning-text">
8788
</tbody>
8889
</table>
8990
</div>
91+
92+
</div>
93+
94+
<div class="card mt-2">
95+
96+
<div class="card-header">
97+
<i class="fas fa-tools"></i> <b>{{ .Service }}.json</b>
98+
</div>
99+
100+
<div class="card-body">
101+
102+
<table class="table table-responsive-sm table-bordered table-striped text-center">
103+
<tbody>
104+
<tr>
105+
<td>Listener</td>
106+
<td><code>{{ .ServiceConfig.Listener }}</code></td>
107+
</tr>
108+
<tr>
109+
<td>Port</td>
110+
<td><code>{{ .ServiceConfig.Port }}</code></td>
111+
</tr>
112+
<tr>
113+
<td>Host</td>
114+
<td><code>{{ .ServiceConfig.Host }}</code></td>
115+
</tr>
116+
<tr>
117+
<td>Auth</td>
118+
<td><code>{{ .ServiceConfig.Auth }}</code></td>
119+
</tr>
120+
<tr>
121+
<td>Logging</td>
122+
<td><code>{{ .ServiceConfig.Logging }}</code></td>
123+
</tr>
124+
</tbody>
125+
</table>
126+
</div>
127+
90128
</div>
91129

92130
<div class="modal fade" id="addSettingModal" tabindex="-1" role="dialog" aria-labelledby="addSettingModal" aria-hidden="true">

cmd/admin/types-templates.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/javuto/osctrl/pkg/nodes"
66
"github.com/javuto/osctrl/pkg/queries"
77
"github.com/javuto/osctrl/pkg/settings"
8+
"github.com/javuto/osctrl/pkg/types"
89
"github.com/javuto/osctrl/pkg/users"
910
)
1011

@@ -128,6 +129,7 @@ type SettingsTemplateData struct {
128129
Environments []environments.TLSEnvironment
129130
Platforms []string
130131
CurrentSettings []settings.SettingValue
132+
ServiceConfig types.JSONConfigurationService
131133
TLSDebug bool
132134
AdminDebug bool
133135
AdminDebugHTTP bool

cmd/admin/utils.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"strconv"
99
"strings"
1010
"time"
11+
12+
"github.com/javuto/osctrl/pkg/settings"
13+
"github.com/javuto/osctrl/pkg/types"
1114
)
1215

1316
// Constants for seconds
@@ -192,3 +195,26 @@ func removeStringDuplicates(s []string) []string {
192195
}
193196
return s[:i]
194197
}
198+
199+
// Helper to convert from settings values to JSON configuration
200+
func toJSONConfigurationService(values []settings.SettingValue) types.JSONConfigurationService {
201+
var cfg types.JSONConfigurationService
202+
for _, v := range values {
203+
if v.Name == settings.JSONListener {
204+
cfg.Listener = v.String
205+
}
206+
if v.Name == settings.JSONPort {
207+
cfg.Port = v.String
208+
}
209+
if v.Name == settings.JSONHost {
210+
cfg.Host = v.String
211+
}
212+
if v.Name == settings.JSONAuth {
213+
cfg.Auth = v.String
214+
}
215+
if v.Name == settings.JSONLogging {
216+
cfg.Logging = v.String
217+
}
218+
}
219+
return cfg
220+
}

cmd/cli/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func updateSetting(c *cli.Context) error {
9696
case settings.TypeBoolean:
9797
err = settingsmgr.SetBoolean(c.Bool("true"), service, name)
9898
case settings.TypeString:
99-
err = settingsmgr.SetString(c.String("string"), service, name)
99+
err = settingsmgr.SetString(c.String("string"), service, name, false)
100100
}
101101
if err != nil {
102102
return err

cmd/tls/main.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,34 @@ var (
5353
)
5454

5555
// Function to load the configuration file and assign to variables
56-
func loadConfiguration() error {
57-
log.Printf("Loading %s", configurationFile)
56+
func loadConfiguration(file string) (types.JSONConfigurationService, error) {
57+
var cfg types.JSONConfigurationService
58+
log.Printf("Loading %s", file)
5859
// Load file and read config
59-
viper.SetConfigFile(configurationFile)
60+
viper.SetConfigFile(file)
6061
err := viper.ReadInConfig()
6162
if err != nil {
62-
return err
63+
return cfg, err
6364
}
6465
// TLS endpoint values
6566
tlsRaw := viper.Sub(settings.ServiceTLS)
66-
err = tlsRaw.Unmarshal(&tlsConfig)
67+
err = tlsRaw.Unmarshal(&cfg)
6768
if err != nil {
68-
return err
69+
return cfg, err
6970
}
7071
// No errors!
71-
return nil
72+
return cfg, nil
7273
}
7374

7475
// Initialization code
7576
func init() {
77+
var err error
7678
// Logging flags
7779
log.SetFlags(log.Lshortfile)
78-
// Load configuration
79-
if err := loadConfiguration(); err != nil {
80-
log.Fatalf("Error loading configuration %s", err)
80+
// Load TLS configuration
81+
tlsConfig, err = loadConfiguration(configurationFile)
82+
if err != nil {
83+
log.Fatalf("Error loading %s - %s", configurationFile, err)
8184
}
8285
}
8386

@@ -159,6 +162,10 @@ func main() {
159162
log.Fatalf("Failed to add %s to configuration: %v", settings.RefreshSettings, err)
160163
}
161164
}
165+
// Write JSON config to settings
166+
if err := settingsmgr.SetAllJSON(settings.ServiceTLS, tlsConfig.Listener, tlsConfig.Port, tlsConfig.Host, tlsConfig.Auth, tlsConfig.Logging); err != nil {
167+
log.Fatalf("Failed to add JSON values to configuration: %v", err)
168+
}
162169
// multiple listeners channel
163170
finish := make(chan bool)
164171

0 commit comments

Comments
 (0)