populate is used to fill in the fields that are not in JSON First, the ExpiryString parameter is needed to parse expiration timestamps from JSON. The JSON decoder is not able to decode a string time duration to a time.Duration, so this is called when loading the configuration to properly parse and
(cfg *Config)
| 185 | // time.Duration, and the AuthKeyString and RemoteName point to |
| 186 | // valid objects. It returns false otherwise. |
| 187 | func (p *SigningProfile) populate(cfg *Config) error { |
| 188 | if p == nil { |
| 189 | return cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, errors.New("can't parse nil profile")) |
| 190 | } |
| 191 | |
| 192 | var err error |
| 193 | if p.RemoteName == "" && p.AuthRemote.RemoteName == "" { |
| 194 | log.Debugf("parse expiry in profile") |
| 195 | if p.ExpiryString == "" { |
| 196 | return cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, errors.New("empty expiry string")) |
| 197 | } |
| 198 | |
| 199 | dur, err := time.ParseDuration(p.ExpiryString) |
| 200 | if err != nil { |
| 201 | return cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, err) |
| 202 | } |
| 203 | |
| 204 | log.Debugf("expiry is valid") |
| 205 | p.Expiry = dur |
| 206 | |
| 207 | if p.BackdateString != "" { |
| 208 | dur, err = time.ParseDuration(p.BackdateString) |
| 209 | if err != nil { |
| 210 | return cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, err) |
| 211 | } |
| 212 | |
| 213 | p.Backdate = dur |
| 214 | } |
| 215 | |
| 216 | if !p.NotBefore.IsZero() && !p.NotAfter.IsZero() && p.NotAfter.Before(p.NotBefore) { |
| 217 | return cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, err) |
| 218 | } |
| 219 | |
| 220 | if len(p.Policies) > 0 { |
| 221 | for _, policy := range p.Policies { |
| 222 | for _, qualifier := range policy.Qualifiers { |
| 223 | if qualifier.Type != "" && qualifier.Type != "id-qt-unotice" && qualifier.Type != "id-qt-cps" { |
| 224 | return cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, |
| 225 | errors.New("invalid policy qualifier type")) |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } else if p.RemoteName != "" { |
| 231 | log.Debug("match remote in profile to remotes section") |
| 232 | if p.AuthRemote.RemoteName != "" { |
| 233 | log.Error("profile has both a remote and an auth remote specified") |
| 234 | return cferr.New(cferr.PolicyError, cferr.InvalidPolicy) |
| 235 | } |
| 236 | if remote := cfg.Remotes[p.RemoteName]; remote != "" { |
| 237 | if err := p.updateRemote(remote); err != nil { |
| 238 | return err |
| 239 | } |
| 240 | } else { |
| 241 | return cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, |
| 242 | errors.New("failed to find remote in remotes section")) |
| 243 | } |
| 244 | } else { |