Ejemplo n.º 1
0
// responseMatchesGeneral is an abstraction of responseMatches and
// responseMatchesInsecure that simply varies in the security of the connection
func responseMatchesGeneral(parameters []string, secure bool) (exitCode int, exitMessage string) {
	urlstr := parameters[0]
	re := wrkutils.ParseUserRegex(parameters[1])
	body := wrkutils.URLToBytes(urlstr, secure)
	if re.Match(body) {
		return 0, ""
	}
	msg := "Response didn't match regexp"
	return wrkutils.GenericError(msg, re.String(), []string{string(body)})
}
Ejemplo n.º 2
0
// checklistsFromDir reads data retrieved from the URL and parses its utf8
// encoded json data, turning it into a checklist struct. It also caches this
// data at remoteCheckDir, currently "/var/run/distributive/"
func checklistFromURL(urlstr string) (chklst Checklist) {
	// ensure temp files dir exists
	log.Debug("Creating/checking remote checklist dir")
	if err := os.MkdirAll(remoteCheckDir, 0775); err != nil {
		log.WithFields(log.Fields{
			"dir":   remoteCheckDir,
			"error": err.Error(),
		}).Warn("Could not create remote check directory")
		remoteCheckDir = "./.remote-checks"
		if err := os.MkdirAll(remoteCheckDir, 0755); err != nil {
			wrkutils.CouldntWriteError(remoteCheckDir, err)
		}
	}
	log.Debug("Using " + remoteCheckDir + " for remote check storage")

	// pathSanitize filters these (path illegal) chars: /?%*:|<^>. \
	pathSanitize := func(str string) (filename string) {
		filename = str
		disallowed := []string{
			`/`, `?`, `%`, `*`, `:`, `|`, `"`, `<`, `^`, `>`, `.`, `\`, ` `,
		}
		for _, c := range disallowed {
			filename = strings.Replace(filename, c, "", -1)
		}
		return filename
	}
	filename := pathSanitize(urlstr) + ".json"
	fullpath := filepath.Join(remoteCheckDir, filename)

	// only create it if it doesn't exist
	if _, err := os.Stat(fullpath); err != nil {
		log.Info("Fetching remote checklist")
		body := wrkutils.URLToBytes(urlstr, true) // secure connection
		log.Debug("Writing remote checklist to cache")
		wrkutils.BytesToFile(body, fullpath)
		return checklistFromBytes(body)
	}
	log.WithFields(log.Fields{
		"path": fullpath,
	}).Info("Using local copy of remote checklist")
	return checklistFromFile(fullpath)
}