|
| 1 | +/* |
| 2 | +Copyright 2024 HAProxy Technologies |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package parsers |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/haproxytech/config-parser/v5/common" |
| 23 | + "github.com/haproxytech/config-parser/v5/errors" |
| 24 | + "github.com/haproxytech/config-parser/v5/types" |
| 25 | +) |
| 26 | + |
| 27 | +type LoadCert struct { |
| 28 | + data []types.LoadCert |
| 29 | + preComments []string // comments that appear before the actual line |
| 30 | +} |
| 31 | + |
| 32 | +func (p *LoadCert) parseError(line string) *errors.ParseError { |
| 33 | + return &errors.ParseError{Parser: "LoadCert", Line: line} |
| 34 | +} |
| 35 | + |
| 36 | +func (p *LoadCert) parse(line string, parts []string, comment string) (*types.LoadCert, error) { |
| 37 | + if len(parts) < 3 { |
| 38 | + return nil, p.parseError(line) |
| 39 | + } |
| 40 | + if parts[0] != "load" { |
| 41 | + return nil, p.parseError(line) |
| 42 | + } |
| 43 | + |
| 44 | + load := new(types.LoadCert) |
| 45 | + |
| 46 | + for i := 1; i < len(parts); i++ { |
| 47 | + element := parts[i] |
| 48 | + switch element { |
| 49 | + case "crt": |
| 50 | + CheckParsePair(parts, &i, &load.Certificate) |
| 51 | + case "alias": |
| 52 | + CheckParsePair(parts, &i, &load.Alias) |
| 53 | + case "key": |
| 54 | + CheckParsePair(parts, &i, &load.Key) |
| 55 | + case "ocsp": |
| 56 | + CheckParsePair(parts, &i, &load.Ocsp) |
| 57 | + case "issuer": |
| 58 | + CheckParsePair(parts, &i, &load.Issuer) |
| 59 | + case "sctl": |
| 60 | + CheckParsePair(parts, &i, &load.Sctl) |
| 61 | + case "ocsp-update": |
| 62 | + i++ |
| 63 | + load.OcspUpdate = new(bool) |
| 64 | + if parts[i] == "on" { |
| 65 | + *load.OcspUpdate = true |
| 66 | + } else if parts[i] != "off" { |
| 67 | + return nil, p.parseError(line) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + load.Comment = comment |
| 72 | + |
| 73 | + // crt is mandatory |
| 74 | + if load.Certificate == "" { |
| 75 | + return nil, p.parseError(line) |
| 76 | + } |
| 77 | + |
| 78 | + return load, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (p *LoadCert) Result() ([]common.ReturnResultLine, error) { |
| 82 | + if len(p.data) == 0 { |
| 83 | + return nil, errors.ErrFetch |
| 84 | + } |
| 85 | + |
| 86 | + result := make([]common.ReturnResultLine, len(p.data)) |
| 87 | + sb := new(strings.Builder) |
| 88 | + |
| 89 | + for i, load := range p.data { |
| 90 | + sb.Reset() |
| 91 | + sb.WriteString("load") |
| 92 | + CheckWritePair(sb, "crt", load.Certificate) |
| 93 | + CheckWritePair(sb, "alias", load.Alias) |
| 94 | + CheckWritePair(sb, "key", load.Key) |
| 95 | + CheckWritePair(sb, "ocsp", load.Ocsp) |
| 96 | + CheckWritePair(sb, "issuer", load.Issuer) |
| 97 | + CheckWritePair(sb, "sctl", load.Sctl) |
| 98 | + CheckWritePair(sb, "ocsp-update", fmtOnOff(load.OcspUpdate)) |
| 99 | + |
| 100 | + result[i] = common.ReturnResultLine{ |
| 101 | + Data: sb.String(), |
| 102 | + Comment: load.Comment, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return result, nil |
| 107 | +} |
| 108 | + |
| 109 | +func fmtOnOff(b *bool) string { |
| 110 | + if b == nil { |
| 111 | + return "" |
| 112 | + } |
| 113 | + if *b { |
| 114 | + return "on" |
| 115 | + } |
| 116 | + return "off" |
| 117 | +} |
0 commit comments