k8s: Extract common validators for DRY code
[integration.git] / test / security / k8s / src / check / validators / master / args / args.go
1 package args
2
3 import (
4         "strconv"
5         "strings"
6 )
7
8 const (
9         portLowest  = 1
10         portHighest = 65536
11 )
12
13 // HasSingleFlagArgument checks whether selected flag was used once and has requested argument.
14 func HasSingleFlagArgument(flag string, argument string, params []string) bool {
15         found := filterFlags(params, flag)
16         if len(found) != 1 {
17                 return false
18         }
19
20         _, value := splitKV(found[0], "=")
21         if value != argument {
22                 return false
23         }
24         return true
25 }
26
27 // HasFlagArgumentIncluded checks whether selected flag includes requested argument.
28 func HasFlagArgumentIncluded(flag string, argument string, params []string) bool {
29         found := filterFlags(params, flag)
30         if len(found) != 1 {
31                 return false
32         }
33
34         _, values := splitKV(found[0], "=")
35         for _, v := range strings.Split(values, ",") {
36                 if v == argument {
37                         return true
38                 }
39         }
40         return false
41 }
42
43 // HasSingleFlagNonemptyArgument checks whether selected flag was used once and has non-empty argument.
44 func HasSingleFlagNonemptyArgument(flag string, params []string) bool {
45         found := filterFlags(params, flag)
46         if len(found) != 1 {
47                 return false
48         }
49
50         _, value := splitKV(found[0], "=")
51         if value == "" {
52                 return false
53         }
54         return true
55 }
56
57 // HasSingleFlagValidPort checks whether selected flag has valid port as an argument in given command.
58 func HasSingleFlagValidPort(flag string, params []string) bool {
59         found := filterFlags(params, flag)
60         if len(found) != 1 {
61                 return false
62         }
63
64         _, value := splitKV(found[0], "=")
65         port, err := strconv.Atoi(value) // what about empty parameter?
66         if err != nil {
67                 return false
68         }
69         if port < portLowest || port > portHighest {
70                 return false
71         }
72         return true
73 }
74
75 // HasSingleFlagValidTimeout checks whether selected flag has valid timeout as an argument in given command.
76 func HasSingleFlagValidTimeout(flag string, min int, max int, params []string) bool {
77         found := filterFlags(params, flag)
78         if len(found) != 1 {
79                 return false
80         }
81
82         _, value := splitKV(found[0], "=")
83         timeout, err := strconv.Atoi(value) // what about empty parameter?
84         if err != nil {
85                 return false
86         }
87         if timeout < min || timeout > max {
88                 return false
89         }
90         return true
91 }
92
93 // HasSingleFlagRecommendedNumericArgument checks whether selected flag was used once and has
94 // an argument that is greater or equal than the recommended value for given command.
95 func HasSingleFlagRecommendedNumericArgument(flag string, recommendation int, params []string) bool {
96         found := filterFlags(params, flag)
97         if len(found) != 1 {
98                 return false
99         }
100
101         _, value := splitKV(found[0], "=")
102         arg, err := strconv.Atoi(value) // what about empty parameter?
103         if err != nil {
104                 return false
105         }
106         if arg < recommendation {
107                 return false
108         }
109         return true
110 }
111
112 // filterFlags returns all occurrences of selected flag.
113 func filterFlags(strs []string, flag string) []string {
114         var filtered []string
115         for _, str := range strs {
116                 if strings.HasPrefix(str, flag) {
117                         filtered = append(filtered, str)
118                 }
119         }
120         return filtered
121 }
122
123 // splitKV splits key and value (after first occurrence of separator).
124 func splitKV(s, sep string) (string, string) {
125         ret := strings.SplitN(s, sep, 2)
126         return ret[0], ret[1]
127 }