ocket8888 commented on a change in pull request #5352: URL: https://github.com/apache/trafficcontrol/pull/5352#discussion_r536449591
########## File path: traffic_ops_ort/plugin_verifier/plugin_verifier.go ########## @@ -0,0 +1,287 @@ +package main + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "bufio" + "fmt" + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/traffic_ops_ort/plugin_verifier/config" + "io" + "io/ioutil" + "os" + "path/filepath" + "regexp" + "strings" +) + +var ( + cfg config.Cfg + atsPlugins = make(map[string]int) + pluginChecks = make(map[string]bool) + pluginParams = make(map[string]bool) +) + +// This function accepts config line data from either ATS +// a 'plugin.config' or a 'remap.config' format. +// +// It checks the configuration file line by line and verifies +// that any specified plugin exists in the file system at the +// complete file path or relative to the ATS plugins installation +// directory. Also, any plugin arguments or plugin parameters that +// end in '.config', '.cfg', or '.txt' are assumed to be plugin +// configuration files and they will be verified that the exist +// at the absolute path in the file name or relative to the ATS +// configuration files directory. +// +// Returns '0' if all plugins on the config line successfully verify +// otherwise, returns the the count of plugins that failed to verify. +// +func checkConfigLine(line string, lineNumber int) int { + + pluginErrorCount := 0 + exists := false + verified := false + + log.Debugf("line: %s\n", line) + + // create an array of whitespace delimited fields + l := regexp.MustCompile(`\s+`) + fields := l.Split(line, -1) + length := len(fields) + + log.Debugf("length: %d, fields: %v", length, fields) + + // processing a line from remap.config + if length > 3 && (fields[0] == "map" || + fields[0] == "map_with_recv_port" || + fields[0] == "map_with_referer" || + fields[0] == "reverse_map" || + fields[0] == "redirect" || + fields[0] == "redirect_temporary") { + + for ii := 3; ii < len(fields); ii++ { + if strings.HasPrefix(fields[ii], "@plugin=") { + sa := strings.Split(fields[ii], "=") + key := strings.TrimSpace(sa[1]) + verified, exists = pluginChecks[key] + log.Debugf("Verified plugin '%s', exists: %v\n", key, verified) + if !exists { + verified = verifyPlugin(key) + pluginChecks[key] = verified + } + if !verified { + log.Errorf("the plugin '%s' on line '%d' or near continuation line '%d' is not available to the installed trafficserver.\n", key, + lineNumber, lineNumber) + pluginErrorCount++ + } + } else if strings.HasPrefix(fields[ii], "@pparam") { + // any plugin parameters that end in '.config | .cfg | .txt' are + // assumed to be configuration files and are checked that they + // exist in the filesystem at the absolute location in the name + // or relative to the ATS configuration files directory. + m := regexp.MustCompile(`^*(\.config|\.cfg|\.txt)+`) + sa := strings.Split(fields[ii], "=") + param := strings.TrimSpace(sa[1]) + if m.MatchString(param) { + verified, exists = pluginParams[param] + if !exists { + verified = verifyPluginConfigfile(param) + pluginParams[param] = verified + } + if !verified { + log.Errorf("the plugin config file '%s' on line '%d' or near continuation line '%d' is not available to the installed trafficserver.\n", param, + lineNumber, lineNumber) + pluginErrorCount++ + } + } + } + } + } else { // process a line from plugin.config + + // process a line from plugin.config + if strings.HasSuffix(fields[0], ".so") { + key := strings.TrimSpace(fields[0]) + verified, exists = pluginChecks[key] + if !exists { + verified = verifyPlugin(key) + pluginChecks[key] = verified + } + if !verified { + log.Errorf("the plugin '%s' on line '%d' is not available to the the installed trafficserver.\n", key, + lineNumber) + pluginErrorCount++ + } + } + // Check the arguments in a plugin.config file for possible plugin config files. + // Any plugin argument that ends in '.config | .cfg | .txt' are + // assumed to be configuration files and are checked that they + // exist in the filesystem at the absolute location in the name + // or relative to the ATS configuration files directory. + m := regexp.MustCompile(`^*(\.config|\.cfg|\.txt)+`) + for ii := 1; ii < length; ii++ { Review comment: well yeah, I see that you're starting at 1 - but you could still use `range` if you wanted to slice it, like: ```go for _, field := range fields[1:] { //... } ``` that'll accomplish the same thing. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
