added security context

This commit is contained in:
ston1th 2020-02-01 17:34:51 +01:00
commit 4e92e722fc
3 changed files with 73 additions and 20 deletions

View file

@ -13,7 +13,7 @@ spec:
allowPrivilegeEscalation: false allowPrivilegeEscalation: false
requiredDropCapabilities: ["ALL"] requiredDropCapabilities: ["ALL"]
readOnlyRootFilesystem: true readOnlyRootFilesystem: true
volumes: ["secret"] volumes: ["configMap", "secret"]
hostNetwork: false hostNetwork: false
hostIPC: false hostIPC: false
hostPID: false hostPID: false

View file

@ -20,8 +20,13 @@ spec:
containers: containers:
- name: status-reporter - name: status-reporter
image: git.giftfish.de/ston1th/status-reporter image: git.giftfish.de/ston1th/status-reporter
imagePullPolicy: Always
securityContext:
runAsUser: 1000
runAsGroup: 1000
capabilities:
drop: ["ALL"]
resources: resources:
requests: requests:
cpu: 100m cpu: 100m
memory: 128Mi memory: 128Mi
imagePullPolicy: Always

84
main.go
View file

@ -89,6 +89,8 @@ type Status struct {
type StatusCache struct { type StatusCache struct {
sync.Mutex sync.Mutex
cache map[string]Status cache map[string]Status
comment sync.Mutex
} }
func NewStatusCache() *StatusCache { func NewStatusCache() *StatusCache {
@ -219,6 +221,8 @@ func statusUpdate(obj interface{}, s *StatusCache, sec typedcorev1.SecretInterfa
log.Printf("skipped comment on issue %s for %s/%s", issue, pr.Namespace, pr.Name) log.Printf("skipped comment on issue %s for %s/%s", issue, pr.Namespace, pr.Name)
return return
} }
s.comment.Lock()
defer s.comment.Unlock()
err = postComment(pr, url, issue, secret) err = postComment(pr, url, issue, secret)
if err != nil { if err != nil {
log.Printf("error posting comment for %s issue %s: %v", url, issue, err) log.Printf("error posting comment for %s issue %s: %v", url, issue, err)
@ -332,6 +336,51 @@ type Comment struct {
Body string `json:"body"` Body string `json:"body"`
} }
func parseLink(link string) (next string, last string) {
if len(link) < 2 {
return
}
a := strings.Split(link, ", ")
for _, v := range a {
b := strings.Split(v, "; ")
if len(b) == 2 {
url := b[0][1 : len(b[0])-1]
switch b[1] {
case `rel="next"`:
next = url
case `rel="last"`:
last = url
}
}
}
return
}
func readComments(c *http.Client, url, user, pass, preview string) (found bool, next, last string, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
req.Header.Set(contentType, jsonType)
req.SetBasicAuth(user, pass)
resp, err := c.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("error reading comments: %s", url)
return
}
next, last = parseLink(resp.Header.Get("Link"))
found = bytes.Contains(body, []byte(preview))
return
}
func postComment(pr *PipelineRun, gitUrl, issue string, sec *corev1.Secret) error { func postComment(pr *PipelineRun, gitUrl, issue string, sec *corev1.Secret) error {
u, err := url.Parse(gitUrl) u, err := url.Parse(gitUrl)
if err != nil { if err != nil {
@ -356,28 +405,27 @@ func postComment(pr *PipelineRun, gitUrl, issue string, sec *corev1.Secret) erro
if err != nil { if err != nil {
return err return err
} }
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
req.Header.Set(contentType, jsonType)
req.SetBasicAuth(user, pass)
c := &http.Client{Transport: &http.Transport{ c := &http.Client{Transport: &http.Transport{
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: 30 * time.Second, Timeout: 30 * time.Second,
}).DialContext, }).DialContext,
}} }}
resp, err := c.Do(req) commentsUrl := u.String()
if err != nil { for {
return err found, next, last, err := readComments(c, commentsUrl, user, pass, preview)
} if err != nil {
defer resp.Body.Close() return err
body, err := ioutil.ReadAll(resp.Body) }
if err != nil { if found {
return err return nil
} }
if bytes.Contains(body, []byte(preview)) { if next == "" {
return nil break
}
if commentsUrl == last {
break
}
commentsUrl = next
} }
comment := &Comment{preview} comment := &Comment{preview}
@ -386,7 +434,7 @@ func postComment(pr *PipelineRun, gitUrl, issue string, sec *corev1.Secret) erro
if err != nil { if err != nil {
return err return err
} }
req, err = http.NewRequest("POST", u.String(), buf) req, err := http.NewRequest("POST", u.String(), buf)
if err != nil { if err != nil {
return err return err
} }