-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
149 lines (120 loc) · 6.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"os"
"github.com/lordofthejars/diferencia/core"
"github.com/lordofthejars/diferencia/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "diferencia",
Short: "Interact with Diferencia",
}
func areHttpsClientAttributesCorrect(caCert, clientCert, clientKey string) bool {
return (len(caCert) == 0 && len(clientCert) == 0 && len(clientKey) == 0) || (len(caCert) > 0 && len(clientCert) > 0 && len(clientKey) > 0)
}
func main() {
var port int
var serviceName, primaryURL, secondaryURL, candidateURL, difference string
var allowUnsafeOperations, noiseDetection bool
var storeResults string
var prometheus bool
var prometheusPort int
var headers bool
var ignoreHeadersValues []string
var ignoreValuesOf []string
var ignoreValuesFile string
var logLevel string
var insecureSkipVerify bool
var caCert, clientCert, clientKey string
var levenshteinPercentage int
var forcePlainText, mirroring bool
var returnResult bool
var adminPort int
var cmdStart = &cobra.Command{
Use: "start",
Short: "Start Diferencia",
Long: `start is used to start Diferencia server to start spreading calls across network`,
Run: func(cmd *cobra.Command, args []string) {
config := core.DiferenciaConfiguration{}
config.Port = port
config.Primary = primaryURL
config.Secondary = secondaryURL
config.Candidate = candidateURL
config.StoreResults = storeResults
config.NoiseDetection = noiseDetection
config.AllowUnsafeOperations = allowUnsafeOperations
config.Headers = headers
config.IgnoreHeadersValues = ignoreHeadersValues
config.Prometheus = prometheus
config.PrometheusPort = prometheusPort
config.IgnoreValues = ignoreValuesOf
config.IgnoreValuesFile = ignoreValuesFile
config.InsecureSkipVerify = insecureSkipVerify
config.CaCert = caCert
config.ClientCert = clientCert
config.ClientKey = clientKey
config.AdminPort = adminPort
config.ForcePlainText = forcePlainText
config.LevenshteinPercentage = levenshteinPercentage
config.Mirroring = mirroring
config.ReturnResult = returnResult
differenceMode, err := core.NewDifference(difference)
if err != nil {
logrus.Errorf("Error while setting difference mode. %s", err.Error())
os.Exit(1)
}
config.DifferenceMode = differenceMode
if mirroring && returnResult {
logrus.Errorf("You cannot set Returning Result of comparision and mirroring at the same time.")
os.Exit(1)
}
if !areHttpsClientAttributesCorrect(caCert, clientCert, clientKey) {
logrus.Errorf("Https Client options should either not provided or all of them provided but not only some. caCert: %s, clientCert: %s, clientkey: %s.", caCert, clientCert, clientKey)
os.Exit(1)
}
if noiseDetection && len(secondaryURL) == 0 {
logrus.Errorf("If Noise Detection is enabled, you need to provide a secondary URL as well")
os.Exit(1)
}
if !noiseDetection && (config.IsIgnoreValuesFileSet() || config.IsIgnoreValuesSet()) {
logrus.Infof("ignoreValues or ignoreValuesFile attributes are set but noise detection is disabled, so they are going to be ignored.")
}
config.SetServiceName(serviceName)
log.Initialize(logLevel)
core.StartProxy(&config)
},
}
cmdStart.Flags().IntVar(&port, "port", 8080, "Listening port of Diferencia proxy")
cmdStart.Flags().StringVar(&serviceName, "serviceName", "", "Sets service name under test. By default it takes candidate hostname")
cmdStart.Flags().StringVarP(&primaryURL, "primary", "p", "", "Primary Service URL")
cmdStart.Flags().StringVarP(&secondaryURL, "secondary", "s", "", "Secondary Service URL")
cmdStart.Flags().StringVarP(&candidateURL, "candidate", "c", "", "Candidate Service URL")
cmdStart.Flags().StringVarP(&difference, "difference", "d", "Strict", "Difference mode to compare JSONs")
cmdStart.Flags().BoolVarP(&allowUnsafeOperations, "unsafe", "u", false, "Allow none safe operations like PUT, POST, PATCH, ...")
cmdStart.Flags().BoolVarP(&noiseDetection, "noisedetection", "n", false, "Enable noise detection. Secondary URL must be provided.")
cmdStart.Flags().StringVar(&storeResults, "storeResults", "", "Directory where output is set. If not specified then nothing is stored. Useful for local development.")
cmdStart.Flags().StringVarP(&logLevel, "logLevel", "l", "error", "Set log level")
cmdStart.Flags().BoolVar(&headers, "headers", false, "Enable Http headers comparision")
cmdStart.Flags().StringSliceVar(&ignoreHeadersValues, "ignoreHeadersValues", nil, "List of headers key where their value must be ignored for comparision purposes.")
cmdStart.Flags().StringSliceVar(&ignoreValuesOf, "ignoreValues", nil, "List of JSON Pointers of values that must be ignored for comparision purposes.")
cmdStart.Flags().StringVar(&ignoreValuesFile, "ignoreValuesFile", "", "File location where each line is a JSON pointers definition for ignoring values.")
cmdStart.Flags().BoolVar(&prometheus, "prometheus", false, "Enable Prometheus endpoint")
cmdStart.Flags().IntVar(&prometheusPort, "prometheusPort", 8081, "Prometheus port")
cmdStart.Flags().IntVar(&adminPort, "adminPort", 8082, "Admin port")
cmdStart.Flags().BoolVar(&insecureSkipVerify, "insecureSkipVerify", false, "Sets Insecure Skip Verify flag in Http Client")
cmdStart.Flags().StringVar(&caCert, "caCert", "", "Certificate Authority path (PEM)")
cmdStart.Flags().StringVar(&clientCert, "clientCert", "", "Client Certificate path (X509)")
cmdStart.Flags().StringVar(&clientKey, "clientKey", "", "Client Key path (X509)")
cmdStart.Flags().BoolVar(&forcePlainText, "forcePlainText", false, "Force the received of content type as plain text instead of json")
cmdStart.Flags().IntVar(&levenshteinPercentage, "levenshteinPercentage", 100, "Sets the minimum percentage to be equal in case of using plain text (40, 79, 90, ...)")
cmdStart.Flags().BoolVarP(&mirroring, "mirroring", "m", false, "Starts Diferencia in mirroring mode which means that the output provided is the one provided by primary")
cmdStart.Flags().BoolVar(&returnResult, "returnResult", false, "Set Diferencia to return all avalable information about the current comparision and not only the http status code.")
cmdStart.MarkFlagRequired("primary")
cmdStart.MarkFlagRequired("candidate")
rootCmd.AddCommand(cmdStart)
if err := rootCmd.Execute(); err != nil {
logrus.Errorf(err.Error())
os.Exit(1)
}
}