1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 05:40:11 +01:00

Added default publisher if not configured. Warning if uploads don't have the same publisher as in metadata.

This commit is contained in:
Sascha L. Teichmann 2021-12-02 10:51:25 +01:00
parent e5a6a8e2da
commit f77bb5f1a8
4 changed files with 66 additions and 8 deletions

View file

@ -113,5 +113,13 @@ func loadConfig() (*config, error) {
cfg.OpenPGPURL = defaultOpenPGPURL cfg.OpenPGPURL = defaultOpenPGPURL
} }
if cfg.Publisher == nil {
cfg.Publisher = &csaf.Publisher{
Category: func(c csaf.Category) *csaf.Category { return &c }(csaf.CSAFCategoryVendor),
Name: func(s string) *string { return &s }("ACME"),
Namespace: func(s string) *string { return &s }("https://example.com"),
}
}
return &cfg, nil return &cfg, nil
} }

View file

@ -215,6 +215,9 @@ func (c *controller) upload(rw http.ResponseWriter, r *http.Request) {
return return
} }
var warnings []string
warn := func(msg string) { warnings = append(warnings, msg) }
if err := doTransaction( if err := doTransaction(
c.cfg, t, c.cfg, t,
func(folder string, pmd *csaf.ProviderMetadata) error { func(folder string, pmd *csaf.ProviderMetadata) error {
@ -328,14 +331,23 @@ func (c *controller) upload(rw http.ResponseWriter, r *http.Request) {
} }
// Take over publisher // Take over publisher
// TODO: Check for conflicts. switch {
pmd.Publisher = ex.publisher case pmd.Publisher == nil:
warn("Publisher in provider metadata is not initialized. Forgot to configure?")
if c.cfg.DynamicProviderMetaData {
warn("Taking publisher from CSAF")
pmd.Publisher = ex.publisher
}
case !pmd.Publisher.Equals(ex.publisher):
warn("Publishers in provider metadata and CSAF do not match.")
}
keyID, fingerprint := key.GetHexKeyID(), key.GetFingerprint() keyID, fingerprint := key.GetHexKeyID(), key.GetFingerprint()
pmd.SetPGP(fingerprint, c.cfg.GetOpenPGPURL(keyID)) pmd.SetPGP(fingerprint, c.cfg.GetOpenPGPURL(keyID))
return nil return nil
}); err != nil { },
); err != nil {
c.failed(rw, "upload.html", err) c.failed(rw, "upload.html", err)
return return
} }
@ -343,6 +355,7 @@ func (c *controller) upload(rw http.ResponseWriter, r *http.Request) {
result := map[string]interface{}{ result := map[string]interface{}{
"Name": newCSAF, "Name": newCSAF,
"ReleaseDate": ex.currentReleaseDate.Format(dateFormat), "ReleaseDate": ex.currentReleaseDate.Format(dateFormat),
"Warnings": warnings,
} }
c.render(rw, "upload.html", result) c.render(rw, "upload.html", result)

View file

@ -14,6 +14,16 @@
<tr><td>CSAF file:</td><td><tt>{{ .Name }}</tt></td></tr> <tr><td>CSAF file:</td><td><tt>{{ .Name }}</tt></td></tr>
<tr><td>Release date:</td><td><tt>{{ .ReleaseDate }}</tt></td></tr> <tr><td>Release date:</td><td><tt>{{ .ReleaseDate }}</tt></td></tr>
</table> </table>
{{ if .Warnings }}
<p>
Warning(s):
<ul>
{{ range .Warnings }}
<li>{{ . }}</li>
{{ end }}
</ul>
</p>
{{ end }}
{{ end }} {{ end }}
<br> <br>
<a href="/cgi-bin/csaf_provider.go/">Back</a>: <a href="/cgi-bin/csaf_provider.go/">Back</a>:

View file

@ -283,20 +283,47 @@ func (r *ROLIE) Validate() error {
// Validate checks if the publisher is valid. // Validate checks if the publisher is valid.
// Returns an error if the validation fails otherwise nil. // Returns an error if the validation fails otherwise nil.
func (cp *Publisher) Validate() error { func (p *Publisher) Validate() error {
switch { switch {
case cp == nil: case p == nil:
return errors.New("publisher is mandatory") return errors.New("publisher is mandatory")
case cp.Category == nil: case p.Category == nil:
return errors.New("publisher.category is mandatory") return errors.New("publisher.category is mandatory")
case cp.Name == nil: case p.Name == nil:
return errors.New("publisher.name is mandatory") return errors.New("publisher.name is mandatory")
case cp.Namespace == nil: case p.Namespace == nil:
return errors.New("publisher.namespace is mandatory") return errors.New("publisher.namespace is mandatory")
} }
return nil return nil
} }
func strPtrEquals(a, b *string) bool {
switch {
case a == nil:
return b == nil
case b == nil:
return false
default:
return *a == *b
}
}
// Equals checks if the publisher is equal to other componentwise.
func (p *Publisher) Equals(o *Publisher) bool {
switch {
case p == nil:
return o == nil
case o == nil:
return false
default:
return strPtrEquals((*string)(p.Category), (*string)(o.Category)) &&
strPtrEquals(p.Name, o.Name) &&
strPtrEquals(p.Namespace, o.Namespace) &&
p.ContactDetails == o.ContactDetails &&
p.IssuingAuthority == o.IssuingAuthority
}
}
// Validate checks if the PGPKey is valid. // Validate checks if the PGPKey is valid.
// Returns an error if the validation fails otherwise nil. // Returns an error if the validation fails otherwise nil.
func (pk *PGPKey) Validate() error { func (pk *PGPKey) Validate() error {