예제 #1
0
func runMigrations(config *app.Config, logger *logrus.Logger) {
	logger.Infof("running migrations in '%s'...", config.DbFile)
	migrator, err := app.NewMigratable(config, logger)
	if err != nil {
		logger.Fatal(err.Error())
	}
	err = migrator.Migrate()
	if err != nil {
		logger.Fatal(err.Error())
	}
}
예제 #2
0
파일: licenses.go 프로젝트: xogeny/soupnazi
// ParseLicenses returns an error if the license
// file is corrupted
func ParseLicenses(lfile string, stream *logrus.Logger) ([]string, error) {
	ret := []string{}
	blank := []string{}

	// If it exists, check that the last
	f, err := os.Open(lfile)
	if err != nil {
		return ret, fmt.Errorf("Error trying to open license file %s: %v", lfile, err)
	}
	defer f.Close()

	stream.Infof("Extracting licenses from %s", lfile)

	reader := bufio.NewReader(f)

	process := func(r io.Reader) (string, error) {
		line, err := reader.ReadString('\n')
		if err != nil {
			return "", err
		}
		stream.Infof("  Raw line: '%s'", line)
		t := strings.Trim(line, "\n")
		stream.Infof("  Trimmed line: '%s'", t)
		return t, nil
	}

	line, err := process(reader)
	if err == io.EOF {
		return ret, nil
	}
	if err != nil {
		return blank, err
	}
	if line != "" {
		ret = append(ret, line)
	}
	for err == nil {
		line, err := process(reader)
		if err == io.EOF {
			return ret, nil
		}
		if err != nil {
			return blank, err
		}
		if line != "" {
			ret = append(ret, line)
		}
	}
	return blank, fmt.Errorf("License file corrupted, ended with %s", line)
}
예제 #3
0
파일: licenses.go 프로젝트: xogeny/soupnazi
func AddLicense(lic string, stream *logrus.Logger) error {
	token := RawToken(lic)

	if token == nil {
		return fmt.Errorf("License %s is not a valid JWT", lic)
	} else {
		stream.Debugf("License passed syntax checking: %v", token)
	}

	lfile := LicenseFile()
	if lfile == "" {
		return fmt.Errorf("Unable to identify settings file")
	}

	stream.Infof("License file location: %s", lfile)

	// If file doesn't exist, "touch" it
	if _, err := os.Stat(lfile); os.IsNotExist(err) {
		pdir := path.Dir(lfile)
		if pdir == "" {
			return fmt.Errorf("Could't determine parent directory of %s: %v", lfile, err)
		}

		stream.Infof("  Creating parent directories")
		err := os.MkdirAll(pdir, 0777)
		if err != nil {
			return fmt.Errorf("Could't create parent directory %s: %v", pdir, err)
		}

		stream.Infof("  Creating license file")
		f, err := os.Create(lfile)
		if err != nil {
			return fmt.Errorf("Error trying create new licenses file at %s: %v", lfile, err)
		}
		f.Close()
	}

	lics, err := ParseLicenses(lfile, stream)
	if err != nil {
		return fmt.Errorf("License file at %s is corrupted: %v")
	}

	// Check for duplicates
	for _, l := range lics {
		if lic == l {
			stream.Infof("  Not adding '%s' because it is a duplicate of an existing entry", l)
			return nil
		}
	}

	f, err := os.OpenFile(lfile, os.O_RDWR, 0777)
	if err != nil {
		return fmt.Errorf("Error trying to open license file %s: %v", lfile, err)
	}

	_, err = f.Seek(0, 2)
	if err != nil {
		return fmt.Errorf("Error seeking end of license file %s: %v", lfile, err)
	}

	_, err = f.Write([]byte(fmt.Sprintf("%s\n", lic)))
	if err != nil {
		return fmt.Errorf("Error writing license to %s: %v", lfile, err)
	}

	return nil
}
예제 #4
0
파일: gen.go 프로젝트: xogeny/soupnazi
func GenerateNodeLocked(details NodeLocked, stream *logrus.Logger) (string, error) {
	// Create the token
	token := jwt.New(jwt.SigningMethodHS256)

	key := KeyHash(details.Secret)

	// Set some claims
	token.Claims["app"] = details.Application
	token.Claims["f"] = details.Feature

	mins := details.Minutes

	stream.Infof("Generating token:")

	// If a negative number was explicitly provided, no expiration is
	// specified.  Otherwise, the details.Days parameter is the number
	// of days until expiration
	if mins < 0 {
		stream.Infof("  Token will never expire")
	} else {
		dur := time.Minute * time.Duration(mins)
		exp := time.Now().Add(dur)
		token.Claims["exp"] = exp.Unix()
		stream.Infof("  Token will expire in: %s", dur.String())
		stream.Infof("    (on %v)", exp.Format(time.UnixDate))
	}

	if details.MAC == "*" {
		stream.Infof("  Token will work for: any MAC address")
	} else {
		stream.Infof("  Token will only work for MAC address: %s", details.MAC)
		token.Claims["a"] = details.MAC
	}

	pstr := ""
	sep := ""
	for k, v := range details.Params {
		pstr = pstr + sep + k + "=" + v
		sep = ", "
	}

	stream.Infof("  Params: %s", pstr)
	if len(details.Params) > 0 {
		token.Claims["p"] = details.Params
	}

	stream.Infof("  Claims: %v", token.Claims)

	// Sign and get the complete encoded token as a string
	tokenString, err := token.SignedString([]byte(key))

	if err == nil {
		stream.Infof("  Token: %s", tokenString)
	} else {
		stream.Infof("  Error creating token: %v", err)
	}

	return tokenString, err
}