Example #1
0
// findFile try to find log file
func findFile(file string) string {
	if fsutil.IsExist(file) {
		return file
	}

	configPath := fsutil.ProperPath("FRS", confPaths)

	if configPath == "" {
		return file
	}

	config, err := knf.Read(configPath)
	logDir := config.GetS("data:log-dir")

	if err != nil || logDir == "" {
		return file
	}

	if !strings.Contains(file, ".log") {
		file += ".log"
	}

	if fsutil.CheckPerms("FRS", path.Join(logDir, file)) {
		return path.Join(logDir, file)
	}

	return file
}
Example #2
0
func Init() {
	runtime.GOMAXPROCS(1)

	args, errs := arg.Parse(argMap)

	if len(errs) != 0 {
		printError("Errors while argument parsing:")

		for _, err := range errs {
			printError("  %v", err)
		}

		os.Exit(1)
	}

	if arg.GetB(ARG_NO_COLOR) {
		fmtc.DisableColors = true
	}

	if arg.GetB(ARG_VER) {
		showAbout()
		return
	}

	if len(args) == 0 || arg.GetB(ARG_HELP) {
		showUsage()
		return
	}

	file := findFile(args[0])

	for {
		if fsutil.CheckPerms("FRS", file) {
			break
		}

		time.Sleep(time.Millisecond * 500)
	}

	if arg.Has(ARG_DATE_FROM) || arg.Has(ARG_DATE_TO) {
		filterFile(file)
	} else {
		readFile(file)
	}
}
Example #3
0
func Make(name string) error {
	if !fsutil.IsWritable(knf.GetS(DATA_RULE_DIR)) {
		return fmt.Errorf("Directory %s must be writable.", knf.GetS(DATA_RULE_DIR))
	}

	if name == "" {
		return errors.New("You must difine mock file name (service1/mock1 for example)")
	}

	if !strings.Contains(name, "/") {
		return errors.New("You must difine mock file name as <service-id>/<mock-name>.")
	}

	template := knf.GetS(TEMPLATE_PATH)
	ruleDir := knf.GetS(DATA_RULE_DIR)
	dirName := path.Dir(name)
	fullPath := path.Join(ruleDir, name)

	if !strings.Contains(fullPath, ".mock") {
		fullPath += ".mock"
	}

	if fsutil.IsExist(fullPath) {
		return fmt.Errorf("File %s already exist\n", fullPath)
	}

	if template == "" || !fsutil.CheckPerms("FRS", template) {
		return createMock(MOCK_TEMPLATE, dirName, fullPath)
	}

	templData, err := ioutil.ReadFile(template)

	if err != nil {
		return fmt.Errorf("Can't read template content from %s: %v", template, err)
	}

	return createMock(string(templData), dirName, fullPath)
}
Example #4
0
func validateConfig() []error {
	var permsChecker = func(config *knf.Config, prop string, value interface{}) error {
		if !fsutil.CheckPerms(value.(string), config.GetS(prop)) {
			switch value.(string) {
			case "DRX":
				return fmt.Errorf("Property %s must be path to readable directory.", prop)
			}
		}

		return nil
	}

	var userChecker = func(config *knf.Config, prop string, value interface{}) error {
		if config.GetS(prop) == "" {
			return nil
		}

		if !system.IsUserExist(config.GetS(prop)) {
			return fmt.Errorf("User defined in %s is not exist.", prop)
		}

		return nil
	}

	var groupChecker = func(config *knf.Config, prop string, value interface{}) error {
		if config.GetS(prop) == "" {
			return nil
		}

		if !system.IsGroupExist(config.GetS(prop)) {
			return fmt.Errorf("Group defined in %s is not exist.", prop)
		}

		return nil
	}

	return knf.Validate([]*knf.Validator{
		&knf.Validator{DATA_RULE_DIR, knf.Empty, nil},
		&knf.Validator{DATA_LOG_DIR, knf.Empty, nil},
		&knf.Validator{DATA_CHECK_DELAY, knf.Empty, nil},
		&knf.Validator{HTTP_PORT, knf.Empty, nil},
		&knf.Validator{HTTP_READ_TIMEOUT, knf.Empty, nil},
		&knf.Validator{HTTP_WRITE_TIMEOUT, knf.Empty, nil},
		&knf.Validator{HTTP_MAX_HEADER_SIZE, knf.Empty, nil},
		&knf.Validator{ACCESS_MOCK_PERMS, knf.Empty, nil},
		&knf.Validator{ACCESS_LOG_PERMS, knf.Empty, nil},
		&knf.Validator{ACCESS_MOCK_DIR_PERMS, knf.Empty, nil},
		&knf.Validator{ACCESS_LOG_DIR_PERMS, knf.Empty, nil},

		&knf.Validator{DATA_RULE_DIR, permsChecker, "DRX"},
		&knf.Validator{DATA_LOG_DIR, permsChecker, "DRX"},

		&knf.Validator{DATA_CHECK_DELAY, knf.Less, MIN_CHECK_DELAY},
		&knf.Validator{DATA_CHECK_DELAY, knf.Greater, MAX_CHECK_DELAY},
		&knf.Validator{HTTP_PORT, knf.Less, MIN_PORT},
		&knf.Validator{HTTP_PORT, knf.Greater, MAX_PORT},
		&knf.Validator{HTTP_READ_TIMEOUT, knf.Less, MIN_READ_TIMEOUT},
		&knf.Validator{HTTP_READ_TIMEOUT, knf.Greater, MAX_READ_TIMEOUT},
		&knf.Validator{HTTP_WRITE_TIMEOUT, knf.Less, MIN_WRITE_TIMEOUT},
		&knf.Validator{HTTP_WRITE_TIMEOUT, knf.Greater, MAX_WRITE_TIMEOUT},
		&knf.Validator{HTTP_MAX_HEADER_SIZE, knf.Less, MIN_HEADER_SIZE},
		&knf.Validator{HTTP_MAX_HEADER_SIZE, knf.Greater, MAX_HEADER_SIZE},

		&knf.Validator{ACCESS_USER, userChecker, nil},
		&knf.Validator{ACCESS_GROUP, groupChecker, nil},
	})
}
Example #5
0
// Load load and parse all rules
func (obs *Observer) Load() bool {
	var ok = true

	for _, r := range obs.uriMap {
		if !fsutil.IsExist(r.Path) {
			delete(obs.uriMap, r.Request.URI)
			delete(obs.wcMap, r.Path)
			delete(obs.errMap, r.Path)
			delete(obs.pathMap, r.Path)
			delete(obs.nameMap[r.Service], r.FullName)

			// If no one rule found for service, remove it's own map
			if len(obs.nameMap[r.Service]) == 0 {
				delete(obs.nameMap, r.Service)
			}

			log.Info("Rule %s unloaded (mock file deleted)", r.PrettyPath)

			continue
		}

		mtime, _ := fsutil.GetMTime(r.Path)

		if r.ModTime.UnixNano() != mtime.UnixNano() {
			rule, err := Parse(obs.ruleDir, r.Service, r.Dir, r.Name)

			if err != nil {
				log.Error(err.Error())
				ok = false
				continue
			}

			// URI can be changed, remove rule from uri map anyway
			delete(obs.uriMap, r.Request.URI)
			delete(obs.errMap, r.Path)

			if r.IsWildcard {
				delete(obs.wcMap, r.Path)
			}

			obs.uriMap[rule.Request.URI] = rule
			obs.pathMap[rule.Path] = rule

			if rule.IsWildcard {
				obs.wcMap[rule.Path] = rule
			}

			log.Info("Rule %s reloaded", rule.PrettyPath)
		}
	}

	if !fsutil.CheckPerms("DRX", obs.ruleDir) {
		log.Error("Can't read directory with rules (%s)", obs.ruleDir)
		return false
	}

	rules := fsutil.ListAllFiles(
		obs.ruleDir, true,
		&fsutil.ListingFilter{
			MatchPatterns: []string{"*.mock"},
		},
	)

	if len(rules) == 0 {
		return ok
	}

	if !obs.checkRules(rules) {
		ok = false
	}

	return ok
}