2016-01-27 19:07:58 +00:00
|
|
|
// Copyright 2015 clair authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package v1
|
|
|
|
|
2016-02-01 20:42:57 +00:00
|
|
|
import (
|
2016-02-24 21:21:47 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-02-01 20:42:57 +00:00
|
|
|
"errors"
|
2016-02-02 00:10:10 +00:00
|
|
|
"fmt"
|
2016-02-05 21:22:17 +00:00
|
|
|
"time"
|
2016-02-01 20:42:57 +00:00
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/coreos/clair/utils/types"
|
2016-02-05 21:22:17 +00:00
|
|
|
"github.com/coreos/pkg/capnslog"
|
|
|
|
"github.com/fernet/fernet-go"
|
2016-02-01 20:42:57 +00:00
|
|
|
)
|
|
|
|
|
2016-02-05 21:22:17 +00:00
|
|
|
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "v1")
|
|
|
|
|
2016-01-28 19:52:09 +00:00
|
|
|
type Error struct {
|
2016-01-29 20:18:48 +00:00
|
|
|
Message string `json:"Layer`
|
2016-01-27 19:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Layer struct {
|
2016-01-29 20:18:48 +00:00
|
|
|
Name string `json:"Name,omitempty"`
|
2016-03-16 18:52:32 +00:00
|
|
|
NamespaceName string `json:"NamespaceName,omitempty"`
|
2016-01-29 20:18:48 +00:00
|
|
|
Path string `json:"Path,omitempty"`
|
|
|
|
ParentName string `json:"ParentName,omitempty"`
|
|
|
|
Format string `json:"Format,omitempty"`
|
|
|
|
IndexedByVersion int `json:"IndexedByVersion,omitempty"`
|
|
|
|
Features []Feature `json:"Features,omitempty"`
|
2016-01-27 19:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:42:57 +00:00
|
|
|
func LayerFromDatabaseModel(dbLayer database.Layer, withFeatures, withVulnerabilities bool) Layer {
|
|
|
|
layer := Layer{
|
|
|
|
Name: dbLayer.Name,
|
|
|
|
IndexedByVersion: dbLayer.EngineVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
if dbLayer.Parent != nil {
|
|
|
|
layer.ParentName = dbLayer.Parent.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if dbLayer.Namespace != nil {
|
2016-03-16 18:52:32 +00:00
|
|
|
layer.NamespaceName = dbLayer.Namespace.Name
|
2016-02-01 20:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if withFeatures || withVulnerabilities && dbLayer.Features != nil {
|
|
|
|
for _, dbFeatureVersion := range dbLayer.Features {
|
|
|
|
feature := Feature{
|
2016-03-16 18:52:32 +00:00
|
|
|
Name: dbFeatureVersion.Feature.Name,
|
|
|
|
NamespaceName: dbFeatureVersion.Feature.Namespace.Name,
|
|
|
|
Version: dbFeatureVersion.Version.String(),
|
|
|
|
AddedBy: dbFeatureVersion.AddedBy.Name,
|
2016-02-01 20:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbVuln := range dbFeatureVersion.AffectedBy {
|
|
|
|
vuln := Vulnerability{
|
2016-03-16 18:52:32 +00:00
|
|
|
Name: dbVuln.Name,
|
|
|
|
NamespaceName: dbVuln.Namespace.Name,
|
|
|
|
Description: dbVuln.Description,
|
|
|
|
Link: dbVuln.Link,
|
|
|
|
Severity: string(dbVuln.Severity),
|
|
|
|
Metadata: dbVuln.Metadata,
|
2016-02-01 20:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if dbVuln.FixedBy != types.MaxVersion {
|
|
|
|
vuln.FixedBy = dbVuln.FixedBy.String()
|
|
|
|
}
|
|
|
|
feature.Vulnerabilities = append(feature.Vulnerabilities, vuln)
|
|
|
|
}
|
|
|
|
layer.Features = append(layer.Features, feature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer
|
|
|
|
}
|
|
|
|
|
2016-03-16 18:52:32 +00:00
|
|
|
type Namespace struct {
|
|
|
|
Name string `json:"Name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
type Vulnerability struct {
|
2016-03-16 18:52:32 +00:00
|
|
|
Name string `json:"Name,omitempty"`
|
|
|
|
NamespaceName string `json:"NamespaceName,omitempty"`
|
|
|
|
Description string `json:"Description,omitempty"`
|
|
|
|
Link string `json:"Link,omitempty"`
|
|
|
|
Severity string `json:"Severity,omitempty"`
|
|
|
|
Metadata map[string]interface{} `json:"Metadata,omitempty"`
|
|
|
|
FixedBy string `json:"FixedBy,omitempty"`
|
|
|
|
FixedIn []Feature `json:"FixedIn,omitempty"`
|
2016-01-27 19:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:42:57 +00:00
|
|
|
func (v Vulnerability) DatabaseModel() (database.Vulnerability, error) {
|
|
|
|
severity := types.Priority(v.Severity)
|
|
|
|
if !severity.IsValid() {
|
|
|
|
return database.Vulnerability{}, errors.New("Invalid severity")
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbFeatures []database.FeatureVersion
|
|
|
|
for _, feature := range v.FixedIn {
|
2016-02-04 19:02:31 +00:00
|
|
|
dbFeature, err := feature.DatabaseModel()
|
2016-02-01 20:42:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return database.Vulnerability{}, err
|
|
|
|
}
|
|
|
|
|
2016-02-04 19:02:31 +00:00
|
|
|
dbFeatures = append(dbFeatures, dbFeature)
|
2016-02-01 20:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return database.Vulnerability{
|
|
|
|
Name: v.Name,
|
2016-03-16 18:52:32 +00:00
|
|
|
Namespace: database.Namespace{Name: v.NamespaceName},
|
2016-02-01 20:42:57 +00:00
|
|
|
Description: v.Description,
|
|
|
|
Link: v.Link,
|
|
|
|
Severity: severity,
|
2016-02-01 23:41:40 +00:00
|
|
|
Metadata: v.Metadata,
|
2016-02-01 20:42:57 +00:00
|
|
|
FixedIn: dbFeatures,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func VulnerabilityFromDatabaseModel(dbVuln database.Vulnerability, withFixedIn bool) Vulnerability {
|
|
|
|
vuln := Vulnerability{
|
2016-03-16 18:52:32 +00:00
|
|
|
Name: dbVuln.Name,
|
|
|
|
NamespaceName: dbVuln.Namespace.Name,
|
|
|
|
Description: dbVuln.Description,
|
|
|
|
Link: dbVuln.Link,
|
|
|
|
Severity: string(dbVuln.Severity),
|
|
|
|
Metadata: dbVuln.Metadata,
|
2016-02-01 20:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if withFixedIn {
|
|
|
|
for _, dbFeatureVersion := range dbVuln.FixedIn {
|
2016-02-04 21:49:38 +00:00
|
|
|
vuln.FixedIn = append(vuln.FixedIn, FeatureFromDatabaseModel(dbFeatureVersion))
|
2016-02-01 20:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vuln
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
type Feature struct {
|
2016-01-29 20:18:48 +00:00
|
|
|
Name string `json:"Name,omitempty"`
|
2016-03-16 18:52:32 +00:00
|
|
|
NamespaceName string `json:"NamespaceName,omitempty"`
|
2016-01-29 20:18:48 +00:00
|
|
|
Version string `json:"Version,omitempty"`
|
|
|
|
Vulnerabilities []Vulnerability `json:"Vulnerabilities,omitempty"`
|
2016-02-18 16:04:16 +00:00
|
|
|
AddedBy string `json:"AddedBy,omitempty"`
|
2016-01-27 19:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 21:49:38 +00:00
|
|
|
func FeatureFromDatabaseModel(dbFeatureVersion database.FeatureVersion) Feature {
|
|
|
|
versionStr := dbFeatureVersion.Version.String()
|
|
|
|
if versionStr == types.MaxVersion.String() {
|
|
|
|
versionStr = "None"
|
|
|
|
}
|
|
|
|
|
|
|
|
return Feature{
|
2016-03-16 18:52:32 +00:00
|
|
|
Name: dbFeatureVersion.Feature.Name,
|
|
|
|
NamespaceName: dbFeatureVersion.Feature.Namespace.Name,
|
|
|
|
Version: versionStr,
|
|
|
|
AddedBy: dbFeatureVersion.AddedBy.Name,
|
2016-02-04 21:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 19:02:31 +00:00
|
|
|
func (f Feature) DatabaseModel() (database.FeatureVersion, error) {
|
2016-02-04 21:49:38 +00:00
|
|
|
var version types.Version
|
|
|
|
if f.Version == "None" {
|
|
|
|
version = types.MaxVersion
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
version, err = types.NewVersion(f.Version)
|
|
|
|
if err != nil {
|
|
|
|
return database.FeatureVersion{}, err
|
|
|
|
}
|
2016-02-04 19:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return database.FeatureVersion{
|
|
|
|
Feature: database.Feature{
|
|
|
|
Name: f.Name,
|
2016-03-16 18:52:32 +00:00
|
|
|
Namespace: database.Namespace{Name: f.NamespaceName},
|
2016-02-04 19:02:31 +00:00
|
|
|
},
|
|
|
|
Version: version,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:07:58 +00:00
|
|
|
type Notification struct {
|
2016-02-02 00:10:10 +00:00
|
|
|
Name string `json:"Name,omitempty"`
|
|
|
|
Created string `json:"Created,omitempty"`
|
|
|
|
Notified string `json:"Notified,omitempty"`
|
|
|
|
Deleted string `json:"Deleted,omitempty"`
|
|
|
|
Limit int `json:"Limit,omitempty"`
|
|
|
|
Page string `json:"Page,omitempty"`
|
|
|
|
NextPage string `json:"NextPage,omitempty"`
|
|
|
|
Old *VulnerabilityWithLayers `json:"Old,omitempty"`
|
2016-02-04 16:34:01 +00:00
|
|
|
New *VulnerabilityWithLayers `json:"New,omitempty"`
|
2016-02-02 00:10:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 21:03:54 +00:00
|
|
|
func NotificationFromDatabaseModel(dbNotification database.VulnerabilityNotification, limit int, pageToken string, nextPage database.VulnerabilityNotificationPageNumber, key string) Notification {
|
2016-02-02 00:10:10 +00:00
|
|
|
var oldVuln *VulnerabilityWithLayers
|
|
|
|
if dbNotification.OldVulnerability != nil {
|
2016-02-05 03:11:49 +00:00
|
|
|
v := VulnerabilityWithLayersFromDatabaseModel(*dbNotification.OldVulnerability)
|
|
|
|
oldVuln = &v
|
2016-02-02 00:10:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
var newVuln *VulnerabilityWithLayers
|
|
|
|
if dbNotification.NewVulnerability != nil {
|
2016-02-05 03:11:49 +00:00
|
|
|
v := VulnerabilityWithLayersFromDatabaseModel(*dbNotification.NewVulnerability)
|
|
|
|
newVuln = &v
|
2016-02-04 16:34:01 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:22:04 +00:00
|
|
|
var nextPageStr string
|
|
|
|
if nextPage != database.NoVulnerabilityNotificationPage {
|
2016-03-07 06:52:26 +00:00
|
|
|
nextPageBytes, _ := tokenMarshal(nextPage, key)
|
|
|
|
nextPageStr = string(nextPageBytes)
|
2016-02-02 00:22:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 15:55:38 +00:00
|
|
|
var created, notified, deleted string
|
|
|
|
if !dbNotification.Created.IsZero() {
|
|
|
|
created = fmt.Sprintf("%d", dbNotification.Created.Unix())
|
|
|
|
}
|
|
|
|
if !dbNotification.Notified.IsZero() {
|
|
|
|
notified = fmt.Sprintf("%d", dbNotification.Notified.Unix())
|
|
|
|
}
|
|
|
|
if !dbNotification.Deleted.IsZero() {
|
|
|
|
deleted = fmt.Sprintf("%d", dbNotification.Deleted.Unix())
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:10:10 +00:00
|
|
|
// TODO(jzelinskie): implement "changed" key
|
2016-02-05 15:55:38 +00:00
|
|
|
fmt.Println(dbNotification.Deleted.IsZero())
|
2016-02-02 00:10:10 +00:00
|
|
|
return Notification{
|
|
|
|
Name: dbNotification.Name,
|
2016-02-05 15:55:38 +00:00
|
|
|
Created: created,
|
|
|
|
Notified: notified,
|
|
|
|
Deleted: deleted,
|
2016-02-02 00:10:10 +00:00
|
|
|
Limit: limit,
|
2016-02-24 21:03:54 +00:00
|
|
|
Page: pageToken,
|
2016-02-02 00:22:04 +00:00
|
|
|
NextPage: nextPageStr,
|
2016-02-02 00:10:10 +00:00
|
|
|
Old: oldVuln,
|
2016-02-04 16:34:01 +00:00
|
|
|
New: newVuln,
|
2016-02-02 00:10:10 +00:00
|
|
|
}
|
2016-01-27 19:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type VulnerabilityWithLayers struct {
|
2016-01-29 20:32:54 +00:00
|
|
|
Vulnerability *Vulnerability `json:"Vulnerability,omitempty"`
|
|
|
|
LayersIntroducingVulnerability []string `json:"LayersIntroducingVulnerability,omitempty"`
|
2016-01-27 19:07:58 +00:00
|
|
|
}
|
2016-01-29 21:43:17 +00:00
|
|
|
|
2016-02-02 00:10:10 +00:00
|
|
|
func VulnerabilityWithLayersFromDatabaseModel(dbVuln database.Vulnerability) VulnerabilityWithLayers {
|
|
|
|
vuln := VulnerabilityFromDatabaseModel(dbVuln, true)
|
|
|
|
|
|
|
|
var layers []string
|
|
|
|
for _, layer := range dbVuln.LayersIntroducingVulnerability {
|
|
|
|
layers = append(layers, layer.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return VulnerabilityWithLayers{
|
|
|
|
Vulnerability: &vuln,
|
|
|
|
LayersIntroducingVulnerability: layers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 21:43:17 +00:00
|
|
|
type LayerEnvelope struct {
|
|
|
|
Layer *Layer `json:"Layer,omitempty"`
|
|
|
|
Error *Error `json:"Error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NamespaceEnvelope struct {
|
2016-03-16 18:52:32 +00:00
|
|
|
Namespaces *[]Namespace `json:"Namespaces,omitempty"`
|
|
|
|
Error *Error `json:"Error,omitempty"`
|
2016-01-29 21:43:17 +00:00
|
|
|
}
|
2016-02-01 19:51:52 +00:00
|
|
|
|
|
|
|
type VulnerabilityEnvelope struct {
|
2016-02-26 10:18:45 +00:00
|
|
|
Vulnerability *Vulnerability `json:"Vulnerability,omitempty"`
|
|
|
|
Vulnerabilities *[]Vulnerability `json:"Vulnerabilities,omitempty"`
|
2016-03-08 02:24:29 +00:00
|
|
|
NextPage string `json:"NextPage,omitempty"`
|
2016-02-26 10:18:45 +00:00
|
|
|
Error *Error `json:"Error,omitempty"`
|
2016-02-01 19:51:52 +00:00
|
|
|
}
|
2016-02-02 00:10:10 +00:00
|
|
|
|
|
|
|
type NotificationEnvelope struct {
|
|
|
|
Notification *Notification `json:"Notification,omitempty"`
|
|
|
|
Error *Error `json:"Error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-02-04 19:02:31 +00:00
|
|
|
type FeatureEnvelope struct {
|
|
|
|
Feature *Feature `json:"Feature,omitempty"`
|
|
|
|
Features *[]Feature `json:"Features,omitempty"`
|
|
|
|
Error *Error `json:"Error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:52:26 +00:00
|
|
|
func tokenUnmarshal(token string, key string, v interface{}) error {
|
2016-02-05 21:22:17 +00:00
|
|
|
k, _ := fernet.DecodeKey(key)
|
|
|
|
msg := fernet.VerifyAndDecrypt([]byte(token), time.Hour, []*fernet.Key{k})
|
|
|
|
if msg == nil {
|
2016-03-07 06:52:26 +00:00
|
|
|
return errors.New("invalid or expired pagination token")
|
2016-02-05 21:22:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 06:52:26 +00:00
|
|
|
return json.NewDecoder(bytes.NewBuffer(msg)).Decode(&v)
|
2016-02-02 00:10:10 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 06:52:26 +00:00
|
|
|
func tokenMarshal(v interface{}, key string) ([]byte, error) {
|
2016-02-24 21:21:47 +00:00
|
|
|
var buf bytes.Buffer
|
2016-03-07 06:52:26 +00:00
|
|
|
err := json.NewEncoder(&buf).Encode(v)
|
2016-02-24 21:21:47 +00:00
|
|
|
if err != nil {
|
2016-03-07 06:52:26 +00:00
|
|
|
return nil, err
|
2016-02-24 21:21:47 +00:00
|
|
|
}
|
2016-02-05 21:22:17 +00:00
|
|
|
|
|
|
|
k, _ := fernet.DecodeKey(key)
|
2016-03-07 06:52:26 +00:00
|
|
|
return fernet.EncryptAndSign(buf.Bytes(), k)
|
2016-02-29 08:29:40 +00:00
|
|
|
}
|