Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
b19fcdce
Commit
b19fcdce
authored
Sep 07, 2016
by
Maciej Szulik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix namespace in audit logs
parent
08b6eaff
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
7 deletions
+60
-7
audit.go
pkg/apiserver/audit/audit.go
+5
-6
audit_test.go
pkg/apiserver/audit/audit_test.go
+54
-0
config.go
pkg/genericapiserver/config.go
+1
-1
No files found.
pkg/apiserver/audit/audit.go
View file @
b19fcdce
...
...
@@ -26,7 +26,7 @@ import (
"github.com/pborman/uuid"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api
server
"
utilnet
"k8s.io/kubernetes/pkg/util/net"
)
...
...
@@ -79,22 +79,21 @@ var _ http.Hijacker = &fancyResponseWriterDelegator{}
// 2. the response line containing:
// - the unique id from 1
// - response code
func
WithAudit
(
handler
http
.
Handler
,
requestContextMapper
api
.
RequestContextMapp
er
,
out
io
.
Writer
)
http
.
Handler
{
func
WithAudit
(
handler
http
.
Handler
,
attributeGetter
apiserver
.
RequestAttributeGett
er
,
out
io
.
Writer
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
ctx
,
_
:=
requestContextMapper
.
Get
(
req
)
user
,
_
:=
api
.
UserFrom
(
ctx
)
attribs
:=
attributeGetter
.
GetAttribs
(
req
)
asuser
:=
req
.
Header
.
Get
(
"Impersonate-User"
)
if
len
(
asuser
)
==
0
{
asuser
=
"<self>"
}
namespace
:=
a
pi
.
NamespaceValue
(
ctx
)
namespace
:=
a
ttribs
.
GetNamespace
(
)
if
len
(
namespace
)
==
0
{
namespace
=
"<none>"
}
id
:=
uuid
.
NewRandom
()
.
String
()
fmt
.
Fprintf
(
out
,
"%s AUDIT: id=%q ip=%q method=%q user=%q as=%q namespace=%q uri=%q
\n
"
,
time
.
Now
()
.
Format
(
time
.
RFC3339Nano
),
id
,
utilnet
.
GetClientIP
(
req
),
req
.
Method
,
user
.
GetName
(),
asuser
,
namespace
,
req
.
URL
)
time
.
Now
()
.
Format
(
time
.
RFC3339Nano
),
id
,
utilnet
.
GetClientIP
(
req
),
req
.
Method
,
attribs
.
GetUser
()
.
GetName
(),
asuser
,
namespace
,
req
.
URL
)
respWriter
:=
decorateResponseWriter
(
w
,
out
,
id
)
handler
.
ServeHTTP
(
respWriter
,
req
)
})
...
...
pkg/apiserver/audit/audit_test.go
View file @
b19fcdce
...
...
@@ -18,11 +18,20 @@ package audit
import
(
"bufio"
"bytes"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"reflect"
"regexp"
"strings"
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apiserver"
"k8s.io/kubernetes/pkg/auth/user"
"k8s.io/kubernetes/pkg/util/sets"
)
type
simpleResponseWriter
struct
{
...
...
@@ -56,3 +65,48 @@ func TestConstructResponseWriter(t *testing.T) {
t
.
Errorf
(
"Expected fancyResponseWriterDelegator, got %v"
,
reflect
.
TypeOf
(
v
))
}
}
type
fakeHTTPHandler
struct
{}
func
(
*
fakeHTTPHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
w
.
WriteHeader
(
200
)
}
type
fakeRequestContextMapper
struct
{}
func
(
*
fakeRequestContextMapper
)
Get
(
req
*
http
.
Request
)
(
api
.
Context
,
bool
)
{
return
api
.
WithUser
(
api
.
NewContext
(),
&
user
.
DefaultInfo
{
Name
:
"admin"
}),
true
}
func
(
*
fakeRequestContextMapper
)
Update
(
req
*
http
.
Request
,
context
api
.
Context
)
error
{
return
nil
}
func
TestAudit
(
t
*
testing
.
T
)
{
var
buf
bytes
.
Buffer
attributeGetter
:=
apiserver
.
NewRequestAttributeGetter
(
&
fakeRequestContextMapper
{},
&
apiserver
.
RequestInfoResolver
{
APIPrefixes
:
sets
.
NewString
(
"api"
,
"apis"
),
GrouplessAPIPrefixes
:
sets
.
NewString
(
"api"
)})
handler
:=
WithAudit
(
&
fakeHTTPHandler
{},
attributeGetter
,
&
buf
)
req
,
_
:=
http
.
NewRequest
(
"GET"
,
"/api/v1/namespaces/default/pods"
,
nil
)
req
.
RemoteAddr
=
"127.0.0.1"
handler
.
ServeHTTP
(
httptest
.
NewRecorder
(),
req
)
line
:=
strings
.
Split
(
strings
.
TrimSpace
(
buf
.
String
()),
"
\n
"
)
if
len
(
line
)
!=
2
{
t
.
Fatalf
(
"Unexpected amount of lines in audit log: %d"
,
len
(
line
))
}
match
,
err
:=
regexp
.
MatchString
(
`[\d\:\-\.\+]+ AUDIT: id="[\w-]+" ip="127.0.0.1" method="GET" user="admin" as="<self>" namespace="default" uri="/api/v1/namespaces/default/pods"`
,
line
[
0
])
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error matching first line: %v"
,
err
)
}
if
!
match
{
t
.
Errorf
(
"Unexpected first line of audit: %s"
,
line
[
0
])
}
match
,
err
=
regexp
.
MatchString
(
`[\d\:\-\.\+]+ AUDIT: id="[\w-]+" response="200"`
,
line
[
1
])
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error matching second line: %v"
,
err
)
}
if
!
match
{
t
.
Errorf
(
"Unexpected second line of audit: %s"
,
line
[
1
])
}
}
pkg/genericapiserver/config.go
View file @
b19fcdce
...
...
@@ -384,7 +384,7 @@ func (c Config) New() (*GenericAPIServer, error) {
MaxBackups
:
c
.
AuditLogMaxBackups
,
MaxSize
:
c
.
AuditLogMaxSize
,
}
handler
=
audit
.
WithAudit
(
handler
,
c
.
RequestContextMapp
er
,
writer
)
handler
=
audit
.
WithAudit
(
handler
,
attributeGett
er
,
writer
)
defer
writer
.
Close
()
}
handler
=
apiserver
.
WithImpersonation
(
handler
,
c
.
RequestContextMapper
,
c
.
Authorizer
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment