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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
|
/*
** Zabbix
** Copyright 2001-2022 Zabbix SIA
**
** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
** documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
** rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in all copies or substantial portions
** of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
** WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
** COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
**/
// Package conf provides .conf file loading and unmarshalling
package conf
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"unicode/utf8"
"git.zabbix.com/ap/plugin-support/std"
)
// Meta structure is used to store the 'conf' tag metadata.
type Meta struct {
name string
defaultValue *string
optional bool
min int64
max int64
}
type Suffix struct {
suffix string
factor int
}
var currentConfigPath string
// setCurrentConfigPath sets a path of the current config file.
func setCurrentConfigPath(path string) {
currentConfigPath = path
}
// GetCurrentConfigPath returns a path of the current config file.
func GetCurrentConfigPath() string {
return currentConfigPath
}
func validateParameterName(key []byte) (err error) {
for i, b := range key {
if ('A' > b || b > 'Z') && ('a' > b || b > 'z') && ('0' > b || b > '9') && b != '_' && b != '.' {
return fmt.Errorf("invalid character '%c' at position %d", b, i+1)
}
}
return
}
// parseLine parses parameter configuration line and returns key,value pair.
// The line must have format: <key>[ ]=[ ]<value> where whitespace surrounding
// '=' is optional.
func parseLine(line []byte) (key []byte, value []byte, err error) {
valueStart := bytes.IndexByte(line, '=')
if valueStart == -1 {
return nil, nil, errors.New("missing assignment operator")
}
if key = bytes.TrimSpace(line[:valueStart]); len(key) == 0 {
return nil, nil, errors.New("missing variable name")
}
if err = validateParameterName(key); err != nil {
return
}
return key, bytes.TrimSpace(line[valueStart+1:]), nil
}
// getMeta returns 'conf' tag metadata.
// The metadata has format [name=<name>,][optional,][range=<range>,][default=<default value>]
// where:
// <name> - the parameter name,
// optional - set if the value is optional,
// <range> - the allowed range <min>:<max>, where <min>, <max> values are optional,
// <default value> - the default value. If specified it must always be the last tag.
func getMeta(field reflect.StructField) (meta *Meta, err error) {
m := Meta{name: "", optional: false, min: -1, max: -1}
conf := field.Tag.Get("conf")
loop:
for conf != "" {
tags := strings.SplitN(conf, ",", 2)
fields := strings.SplitN(tags[0], "=", 2)
tag := strings.TrimSpace(fields[0])
if len(fields) == 1 {
// boolean tag
switch tag {
case "optional":
m.optional = true
default:
return nil, fmt.Errorf("invalid conf tag '%s'", tag)
}
} else {
// value tag
switch tag {
case "default":
value := fields[1]
if len(tags) == 2 {
value += "," + tags[1]
}
m.defaultValue = &value
break loop
case "name":
m.name = strings.TrimSpace(fields[1])
case "range":
limits := strings.Split(fields[1], ":")
if len(limits) != 2 {
return nil, errors.New("invalid range format")
}
if limits[0] != "" {
m.min, _ = strconv.ParseInt(limits[0], 10, 64)
}
if limits[1] != "" {
m.max, _ = strconv.ParseInt(limits[1], 10, 64)
}
default:
return nil, fmt.Errorf("invalid conf tag '%s'", tag)
}
}
if len(tags) == 1 {
break
}
conf = tags[1]
}
if m.name == "" {
m.name = field.Name
}
return &m, nil
}
func getTimeSuffix(str string) (string, int) {
suffixes := []Suffix{
Suffix{
suffix: "s",
factor: 1,
},
Suffix{
suffix: "m",
factor: 60,
},
Suffix{
suffix: "h",
factor: 3600,
},
Suffix{
suffix: "d",
factor: 86400,
},
Suffix{
suffix: "w",
factor: (7 * 86400),
},
}
for _, s := range suffixes {
if strings.HasSuffix(str, s.suffix) == true {
str = strings.TrimSuffix(str, s.suffix)
return str, s.factor
}
}
return str, 1
}
func setBasicValue(value reflect.Value, meta *Meta, str *string) (err error) {
if str == nil {
return nil
}
switch value.Type().Kind() {
case reflect.String:
value.SetString(*str)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var v int64
var r int
*str, r = getTimeSuffix(*str)
if v, err = strconv.ParseInt(*str, 10, 64); err == nil {
v = v * int64(r)
if meta != nil {
if meta.min != -1 && v < meta.min || meta.max != -1 && v > meta.max {
return errors.New("value out of range")
}
}
value.SetInt(v)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
var v uint64
var r int
*str, r = getTimeSuffix(*str)
if v, err = strconv.ParseUint(*str, 10, 64); err == nil {
v = v * uint64(r)
if meta != nil {
if meta.min != -1 && v < uint64(meta.min) || meta.max != -1 && v > uint64(meta.max) {
return errors.New("value out of range")
}
}
value.SetUint(v)
}
case reflect.Float32, reflect.Float64:
var v float64
if v, err = strconv.ParseFloat(*str, 64); err == nil {
if meta != nil {
if meta.min != -1 && v < float64(meta.min) || meta.max != -1 && v > float64(meta.max) {
return errors.New("value out of range")
}
}
value.SetFloat(v)
}
case reflect.Bool:
var v bool
switch *str {
case "true":
v = true
case "false":
v = false
default:
return errors.New("invalid boolean value")
}
value.SetBool(v)
case reflect.Ptr:
v := reflect.New(value.Type().Elem())
value.Set(v)
return setBasicValue(v.Elem(), meta, str)
default:
err = fmt.Errorf("unsupported variable type %v", value.Type().Kind())
}
return err
}
func setStructValue(value reflect.Value, node *Node) (err error) {
rt := value.Type()
for i := 0; i < rt.NumField(); i++ {
var meta *Meta
if meta, err = getMeta(rt.Field(i)); err != nil {
return
}
child := node.get(meta.name)
if child != nil || meta.defaultValue != nil {
if err = setValue(value.Field(i), meta, child); err != nil {
return
}
} else if !meta.optional {
return fmt.Errorf("cannot find mandatory parameter %s", meta.name)
}
}
return
}
func setMapValue(value reflect.Value, node *Node) (err error) {
m := reflect.MakeMap(reflect.MapOf(value.Type().Key(), value.Type().Elem()))
for _, v := range node.Nodes {
if child, ok := v.(*Node); ok {
k := reflect.New(value.Type().Key())
if err = setBasicValue(k.Elem(), nil, &child.Name); err != nil {
return
}
v := reflect.New(value.Type().Elem())
if err = setValue(v.Elem(), nil, child); err != nil {
return
}
m.SetMapIndex(k.Elem(), v.Elem())
}
}
value.Set(m)
return
}
func setSliceValue(value reflect.Value, node *Node) (err error) {
tmpValues := make([][]byte, 0)
for _, v := range node.Nodes {
if val, ok := v.(*Value); ok {
tmpValues = append(tmpValues, val.Value)
}
}
size := len(tmpValues)
values := reflect.MakeSlice(reflect.SliceOf(value.Type().Elem()), 0, size)
if len(tmpValues) > 0 {
for _, data := range tmpValues {
v := reflect.New(value.Type().Elem())
str := string(data)
if err = setBasicValue(v.Elem(), nil, &str); err != nil {
return
}
values = reflect.Append(values, v.Elem())
}
} else {
for _, n := range node.Nodes {
if child, ok := n.(*Node); ok {
v := reflect.New(value.Type().Elem())
if err = setValue(v.Elem(), nil, child); err != nil {
return
}
values = reflect.Append(values, v.Elem())
}
}
}
value.Set(values)
return
}
func setValue(value reflect.Value, meta *Meta, node *Node) (err error) {
var str *string
if node != nil {
node.used = true
}
switch value.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.Bool, reflect.String:
if str, err = node.getValue(meta); err == nil {
if err = setBasicValue(value, meta, str); err != nil {
return node.newError("%s", err.Error())
}
}
case reflect.Struct:
if node != nil {
return setStructValue(value, node)
}
case reflect.Map:
if node != nil {
return setMapValue(value, node)
}
case reflect.Slice:
if node != nil {
return setSliceValue(value, node)
}
case reflect.Ptr:
v := reflect.New(value.Type().Elem())
value.Set(v)
return setValue(v.Elem(), meta, node)
case reflect.Interface:
value.Set(reflect.ValueOf(node))
node.markUsed(true)
}
return nil
}
// assignValues assigns parsed nodes to the specified structure
func assignValues(v interface{}, root *Node) (err error) {
rv := reflect.ValueOf(v)
switch rv.Type().Kind() {
case reflect.Ptr:
rv = rv.Elem()
default:
return errors.New("output variable must be a pointer to a structure")
}
switch rv.Type().Kind() {
case reflect.Struct:
if err = setStructValue(rv, root); err != nil {
return err
}
default:
return errors.New("output variable must be a pointer to a structure")
}
return root.checkUsage()
}
func newIncludeError(root *Node, filename *string, errmsg string) (err error) {
if root.includeFail {
return errors.New(errmsg)
}
root.includeFail = true
if filename != nil {
return fmt.Errorf(`cannot include "%s": %s`, *filename, errmsg)
}
return fmt.Errorf(`cannot load file: %s`, errmsg)
}
func hasMeta(path string) bool {
var metaChars string
if runtime.GOOS != "windows" {
metaChars = `*?[\`
} else {
metaChars = `*?[`
}
return strings.ContainsAny(path, metaChars)
}
func loadInclude(root *Node, path string) (err error) {
path = filepath.Clean(path)
if err := checkGlobPattern(path); err != nil {
return newIncludeError(root, &path, err.Error())
}
absPath, err := filepath.Abs(path)
if err != nil {
return newIncludeError(root, &path, err.Error())
}
// If a path is relative, pad it with a directory of the current config file
if path != absPath {
confDir := filepath.Dir(GetCurrentConfigPath())
path = filepath.Join(confDir, path)
}
if hasMeta(filepath.Dir(path)) {
return newIncludeError(root, &path, "glob pattern is supported only in file names")
}
if !hasMeta(path) {
var fi os.FileInfo
if fi, err = stdOs.Stat(path); err != nil {
return newIncludeError(root, &path, err.Error())
}
if fi.IsDir() {
path = filepath.Join(path, "*")
}
} else {
var fi os.FileInfo
if fi, err = stdOs.Stat(filepath.Dir(path)); err != nil {
return newIncludeError(root, &path, err.Error())
}
if !fi.IsDir() {
return newIncludeError(root, &path, "base path is not a directory")
}
}
var paths []string
if hasMeta(path) {
if paths, err = filepath.Glob(path); err != nil {
return newIncludeError(root, nil, err.Error())
}
} else {
paths = append(paths, path)
}
for _, path := range paths {
// skip directories
var fi os.FileInfo
if fi, err = stdOs.Stat(path); err != nil {
return newIncludeError(root, &path, err.Error())
}
if fi.IsDir() {
continue
}
var file std.File
if file, err = stdOs.Open(path); err != nil {
return newIncludeError(root, &path, err.Error())
}
defer file.Close()
buf := bytes.Buffer{}
if _, err = buf.ReadFrom(file); err != nil {
return newIncludeError(root, &path, err.Error())
}
if err = parseConfig(root, buf.Bytes()); err != nil {
return newIncludeError(root, &path, err.Error())
}
}
return
}
func checkGlobPattern(path string) error {
if strings.HasPrefix(path, "*") {
return errors.New("path should be absolute")
}
var isGlob, hasSepLeft, hasSepRight bool
for _, p := range path {
switch p {
case '*':
isGlob = true
case filepath.Separator:
switch isGlob {
case true:
hasSepRight = true
case false:
hasSepLeft = true
}
}
}
if (isGlob && !hasSepLeft && hasSepRight) || (isGlob && !hasSepLeft && !hasSepRight) {
return errors.New("path should be absolute")
}
return nil
}
func parseConfig(root *Node, data []byte) (err error) {
const maxStringLen = 2048
var line []byte
root.level++
for offset, end, num := 0, 0, 1; end != -1; offset, num = offset+end+1, num+1 {
if end = bytes.IndexByte(data[offset:], '\n'); end != -1 {
line = bytes.TrimSpace(data[offset : offset+end])
} else {
line = bytes.TrimSpace(data[offset:])
}
if len(line) > maxStringLen {
return fmt.Errorf("cannot parse configuration at line %d: limit of %d bytes is exceeded", num, maxStringLen)
}
if len(line) == 0 || line[0] == '#' {
continue
}
if !utf8.ValidString(string(line)) {
return fmt.Errorf("cannot parse configuration at line %d: not a valid UTF-8 character found", num)
}
var key, value []byte
if key, value, err = parseLine(line); err != nil {
return fmt.Errorf("cannot parse configuration at line %d: %s", num, err.Error())
}
if string(key) == "Include" {
if root.level == 10 {
return fmt.Errorf("include depth exceeded limits")
}
if err = loadInclude(root, string(value)); err != nil {
return
}
} else {
root.add(key, value, num)
}
}
root.level--
return nil
}
func addObject(parent *Node, v interface{}) error {
if attr, ok := v.(map[string]interface{}); ok {
if _, ok := attr["Nodes"]; ok {
node := &Node{}
if err := setObjectNode(node, attr); err != nil {
return err
}
parent.Nodes = append(parent.Nodes, node)
} else {
value := &Value{}
if err := setObjectValue(value, attr); err != nil {
return err
}
parent.Nodes = append(parent.Nodes, value)
}
} else {
return fmt.Errorf("invalid object type %T", v)
}
return nil
}
func setObjectValue(value *Value, attr map[string]interface{}) error {
var line float64
var ok bool
if line, ok = attr["Line"].(float64); !ok {
return fmt.Errorf("invalid line attribute type %T", attr["Line"])
}
value.Line = int(line)
var err error
var data string
if data, ok = attr["Value"].(string); !ok {
return fmt.Errorf("invalid value type %T", attr["Value"])
}
if value.Value, err = base64.StdEncoding.DecodeString(data); err != nil {
return err
}
return nil
}
func setObjectNode(node *Node, attr map[string]interface{}) error {
var line float64
var ok bool
if line, ok = attr["Line"].(float64); !ok {
return fmt.Errorf("invalid line attribute type %T", attr["Line"])
}
node.Line = int(line)
if node.Name, ok = attr["Name"].(string); !ok {
return fmt.Errorf("invalid node name type %T", attr["Name"])
}
var nodes []interface{}
if nodes, ok = attr["Nodes"].([]interface{}); !ok {
return fmt.Errorf("invalid node children type %T", attr["u"])
}
for _, a := range nodes {
if err := addObject(node, a); err != nil {
return err
}
}
return nil
}
// Unmarshal unmarshals input data into specified structure. The input data can be either
// a byte array ([]byte) with configuration file or interface{} either returned by Marshal
// or a configuration file Unmarshaled into interface{} variable before.
// The third is optional 'strict' parameter that forces strict validation of configuration
// and structure fields (enabled by default). When disabled it will unmarshal part of
// configuration into incomplete target structures.
func Unmarshal(data interface{}, v interface{}, args ...interface{}) (err error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("Invalid output parameter")
}
strict := true
if len(args) > 0 {
var ok bool
if strict, ok = args[0].(bool); !ok {
return errors.New("Invalid mode parameter")
}
}
var root *Node
switch u := data.(type) {
case nil:
root = &Node{
Name: "",
used: false,
Nodes: make([]interface{}, 0),
parent: nil,
Line: 0}
case []byte:
root = &Node{
Name: "",
used: false,
Nodes: make([]interface{}, 0),
parent: nil,
Line: 0}
if err = parseConfig(root, u); err != nil {
return fmt.Errorf("Cannot read configuration: %s", err.Error())
}
case *Node:
root = u
root.markUsed(false)
case map[string]interface{}: // JSON unmarshaling result
root = &Node{}
if err = setObjectNode(root, u); err != nil {
return fmt.Errorf("Cannot unmarshal JSON data: %s", err)
}
default:
return fmt.Errorf("Invalid input parameter of type %T", u)
}
if !strict {
root.markUsed(true)
}
if err = assignValues(v, root); err != nil {
return fmt.Errorf("Cannot assign configuration: %s", err.Error())
}
return nil
}
func Load(filename string, v interface{}) (err error) {
var file std.File
if file, err = stdOs.Open(filename); err != nil {
return fmt.Errorf(`cannot open configuration file: %s`, err.Error())
}
defer file.Close()
buf := bytes.Buffer{}
if _, err = buf.ReadFrom(file); err != nil {
return fmt.Errorf("cannot load configuration: %s", err.Error())
}
setCurrentConfigPath(filename)
return Unmarshal(buf.Bytes(), v)
}
func LoadUserParams(v interface{}) (err error) {
var file std.File
if file, err = stdOs.Open(currentConfigPath); err != nil {
return fmt.Errorf(`cannot open configuration file: %s`, err.Error())
}
defer file.Close()
buf := bytes.Buffer{}
if _, err = buf.ReadFrom(file); err != nil {
return fmt.Errorf("cannot load configuration: %s", err.Error())
}
return Unmarshal(buf.Bytes(), v, false)
}
var stdOs std.Os
func init() {
stdOs = std.NewOs()
}
|