mirror of
https://github.com/FairwindsOps/polaris.git
synced 2026-05-11 11:47:12 +00:00
* Start working on updating dependencies: * Fix webhook * Rollback jsonschema update * Checkin new config * Fix run as root * Update versions of kind * Fix typo in kind URL * Fix kind config * Add csr permissions * Fix weird image thing * Fixed certificates * Add to logging * Approve cert manually * Fix approval * Add cert script * Fix deployment * Add requests/limits * Wait if certificate doesn't exist yet * Add check for file size * Add variable * Try a different imagE * Fix command * Update certificate logic * Add healthz * Don't check cert size * Remove stat * Fix vet * Put in change that makes no sense * Fix cert names * Roll back * Try changing config * Add logging for each request * Cleanup code some * Remove bad deployments * Fix client injection * Update timeout * Add logging * Fixed e2e webhook tests * Add permissions for approval * Fix permissions for CSR * Remove logging code * Remove refresh certs file * Fix merge issues * Update deployments * Try beta of admission controller config * Target 1.15 for testing * Add beta versions of resourceS * Lower webhook timeout * Refactor out a method * Fix up PR issues * Fix more tabs * Remove unnecessary messageS * Fix go.sum * Fix go.sum
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package validator
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"time"
|
|
|
|
conf "github.com/fairwindsops/polaris/pkg/config"
|
|
"github.com/fairwindsops/polaris/pkg/kube"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
apiMachineryYAML "k8s.io/apimachinery/pkg/util/yaml"
|
|
)
|
|
|
|
// RunAudit runs a full Polaris audit and returns an AuditData object
|
|
func RunAudit(ctx context.Context, config conf.Configuration, kubeResources *kube.ResourceProvider) (AuditData, error) {
|
|
displayName := config.DisplayName
|
|
if displayName == "" {
|
|
displayName = kubeResources.SourceName
|
|
}
|
|
|
|
results, err := ValidateControllers(ctx, &config, kubeResources)
|
|
if err != nil {
|
|
return AuditData{}, err
|
|
}
|
|
|
|
auditData := AuditData{
|
|
PolarisOutputVersion: PolarisOutputVersion,
|
|
AuditTime: kubeResources.CreationTime.Format(time.RFC3339),
|
|
SourceType: kubeResources.SourceType,
|
|
SourceName: kubeResources.SourceName,
|
|
DisplayName: displayName,
|
|
ClusterInfo: ClusterInfo{
|
|
Version: kubeResources.ServerVersion,
|
|
Nodes: len(kubeResources.Nodes),
|
|
Pods: len(kubeResources.Controllers), // TODO validate that this is still valuable
|
|
Namespaces: len(kubeResources.Namespaces),
|
|
Controllers: len(results),
|
|
},
|
|
Results: results,
|
|
}
|
|
return auditData, nil
|
|
}
|
|
|
|
// ReadAuditFromFile reads the data from a past audit stored in a JSON or YAML file.
|
|
func ReadAuditFromFile(fileName string) AuditData {
|
|
auditData := AuditData{}
|
|
oldFileBytes, err := ioutil.ReadFile(fileName)
|
|
if err != nil {
|
|
logrus.Errorf("Unable to read contents of loaded file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
auditData, err = ParseAudit(oldFileBytes)
|
|
if err != nil {
|
|
logrus.Errorf("Error parsing file contents into auditData: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
return auditData
|
|
}
|
|
|
|
// ParseAudit decodes either a YAML or JSON file and returns AuditData.
|
|
func ParseAudit(oldFileBytes []byte) (AuditData, error) {
|
|
reader := bytes.NewReader(oldFileBytes)
|
|
conf := AuditData{}
|
|
d := apiMachineryYAML.NewYAMLOrJSONDecoder(reader, 4096)
|
|
for {
|
|
if err := d.Decode(&conf); err != nil {
|
|
if err == io.EOF {
|
|
return conf, nil
|
|
}
|
|
return conf, fmt.Errorf("Decoding config failed: %v", err)
|
|
}
|
|
}
|
|
}
|