k8s: Resolve Docker response formatting issue 17/96417/1
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 30 Sep 2019 12:39:32 +0000 (14:39 +0200)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 30 Sep 2019 13:31:13 +0000 (15:31 +0200)
Checker collects information on cluster by Docker queries:

$ docker ps ARGS...      # Casablanca
$ docker inspect ARGS... # Dublin

Arrays of values are then filtered from those. They include:

* opening bracket ('['),
* closing bracket (']'),
* new line.

Additional characters affect check results if last flag (including
"]\n") requires specific value.

Issue-ID: SECCOM-235
Change-Id: I6838342b7e2ecdc44a47ffe02286266003e0b4d3
Signed-off-by: Pawel Wieczorek <p.wieczorek2@samsung.com>
test/security/k8s/src/check/rancher/rancher.go
test/security/k8s/src/check/raw/raw.go

index 842fd3f..b5e3822 100644 (file)
@@ -58,6 +58,7 @@ func getProcessParams(process check.Command, service check.Service) ([]string, e
                        return []string{}, err
                }
 
+               cmd = trimOutput(cmd) // TODO: improve `docker ps` query format.
                if len(cmd) > 0 {
                        i := bytes.Index(cmd, []byte(process.String()))
                        if i == -1 {
@@ -99,6 +100,14 @@ func getPsCmdOutput(host string, service check.Service) ([]byte, error) {
        return out, nil
 }
 
+// trimOutput removes trailing new line and brackets from output.
+func trimOutput(b []byte) []byte {
+       b = bytes.TrimSpace(b)
+       b = bytes.TrimPrefix(b, []byte("["))
+       b = bytes.TrimSuffix(b, []byte("]"))
+       return b
+}
+
 // btos converts slice of bytes to slice of strings split by white space characters.
 func btos(in []byte) []string {
        var out []string
index 04a6fa5..5551159 100644 (file)
@@ -59,6 +59,7 @@ func getProcessParams(process check.Command) ([]string, error) {
                                return []string{}, err
                        }
 
+                       cmd = trimOutput(cmd) // TODO: improve `docker inspect` query format.
                        if len(cmd) > 0 {
                                i := bytes.Index(cmd, []byte(process.String()))
                                if i == -1 {
@@ -158,6 +159,14 @@ func runCommand(cmd string, conn *ssh.Client) ([]byte, error) {
        return out, nil
 }
 
+// trimOutput removes trailing new line and brackets from output.
+func trimOutput(b []byte) []byte {
+       b = bytes.TrimSpace(b)
+       b = bytes.TrimPrefix(b, []byte("["))
+       b = bytes.TrimSuffix(b, []byte("]"))
+       return b
+}
+
 // btos converts slice of bytes to slice of strings split by white space characters.
 func btos(in []byte) []string {
        var out []string