-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from listx/fix-auditor
Fix auditor
- Loading branch information
Showing
8 changed files
with
228 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright 2020 The Kubernetes 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 | ||
https://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 audit | ||
|
||
import ( | ||
"net/url" | ||
|
||
"cloud.google.com/go/errorreporting" | ||
"sigs.k8s.io/k8s-container-image-promoter/lib/logclient" | ||
) | ||
|
||
// ServerContext holds all of the initialization data for the server to start | ||
// up. | ||
type ServerContext struct { | ||
ID string | ||
RepoURL *url.URL | ||
RepoBranch string | ||
ThinManifestDirPath string | ||
ErrorReportingClient *errorreporting.Client | ||
LoggingFacility *logclient.LoggingFacility | ||
} | ||
|
||
// PubSubMessageInner is the inner struct that holds the actual Pub/Sub | ||
// information. | ||
type PubSubMessageInner struct { | ||
Data []byte `json:"data,omitempty"` | ||
ID string `json:"id"` | ||
} | ||
|
||
// PubSubMessage is the payload of a Pub/Sub event. | ||
type PubSubMessage struct { | ||
Message PubSubMessageInner `json:"message"` | ||
Subscription string `json:"subscription"` | ||
} | ||
|
||
const ( | ||
cloneDepth = 1 | ||
// LogName is the auditing log name to use. This is the name that comes up | ||
// for "gcloud logging logs list". | ||
LogName = "cip-audit-log" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"fake.go", | ||
"gcp.go", | ||
"types.go", | ||
], | ||
importpath = "sigs.k8s.io/k8s-container-image-promoter/lib/logclient", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@com_google_cloud_go_logging//:go_default_library", | ||
"@io_k8s_klog//:go_default_library", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright 2020 The Kubernetes 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 | ||
https://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 logclient | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
// FakeLogClient is a fake log client. Its sole purpose is to implement a NOP | ||
// "Close()" method, for tests. | ||
type FakeLogClient struct{} | ||
|
||
// Close is a NOP (there is nothing to close). | ||
func (fakeLogClient *FakeLogClient) Close() error { return nil } | ||
|
||
// NewFakeLoggingFacility returns a new LoggingFacility, but whose resources are | ||
// all local (stdout), with a FakeLogClient. | ||
func NewFakeLoggingFacility() *LoggingFacility { | ||
|
||
logInfo := log.New(os.Stdout, "FAKE-INFO", log.LstdFlags) | ||
logError := log.New(os.Stdout, "FAKE-ERROR", log.LstdFlags) | ||
logAlert := log.New(os.Stdout, "FAKE-ALERT", log.LstdFlags) | ||
|
||
return New(logInfo, logError, logAlert, &FakeLogClient{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2020 The Kubernetes 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 | ||
https://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 logclient | ||
|
||
import ( | ||
"context" | ||
|
||
"cloud.google.com/go/logging" | ||
"k8s.io/klog" | ||
) | ||
|
||
// NewGcpLoggingFacility returns a new LoggingFacility that logs to GCP | ||
// resources. As such, it requires the GCP projectID as well as the logName to | ||
// log to. | ||
func NewGcpLoggingFacility(projectID, logName string) *LoggingFacility { | ||
gcpLogClient := initGcpLogClient(projectID) | ||
|
||
logInfo := gcpLogClient.Logger(logName).StandardLogger(logging.Info) | ||
logError := gcpLogClient.Logger(logName).StandardLogger(logging.Error) | ||
logAlert := gcpLogClient.Logger(logName).StandardLogger(logging.Alert) | ||
|
||
return New(logInfo, logError, logAlert, gcpLogClient) | ||
} | ||
|
||
// initGcpLogClient creates a logging client that performs better logging than | ||
// the default behavior on GCP Stackdriver. For instance, logs sent with this | ||
// client are not split up over newlines, and also the severity levels are | ||
// actually understood by Stackdriver. | ||
func initGcpLogClient(projectID string) *logging.Client { | ||
|
||
ctx := context.Background() | ||
|
||
// Creates a client. | ||
client, err := logging.NewClient(ctx, projectID) | ||
if err != nil { | ||
klog.Fatalf("Failed to create client: %v", err) | ||
} | ||
|
||
return client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2020 The Kubernetes 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 | ||
https://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 logclient | ||
|
||
import ( | ||
"io" | ||
"log" | ||
) | ||
|
||
// LoggingFacility bundles 3 loggers together. | ||
type LoggingFacility struct { | ||
LogInfo *log.Logger | ||
LogError *log.Logger | ||
LogAlert *log.Logger | ||
|
||
logClient io.Closer | ||
} | ||
|
||
// New returns a new LoggingFacility, based on the given loggers. | ||
func New( | ||
logInfo, logError, logAlert *log.Logger, | ||
logClient io.Closer, | ||
) *LoggingFacility { | ||
return &LoggingFacility{ | ||
LogInfo: logInfo, | ||
LogError: logError, | ||
LogAlert: logAlert, | ||
logClient: logClient, | ||
} | ||
} | ||
|
||
// Close implements the "Close" method for the LoggingFacility. This just calls | ||
// Close() on the "logClient". | ||
func (loggingFacility *LoggingFacility) Close() error { | ||
return loggingFacility.logClient.Close() | ||
} |