// AddDir adds a directory to be watched, returning an error if any. // It allows a filter to be specified on which files to watch. // It also allows recursive watching. func (fw *FileSystemWatcher) AddDir(path, pattern string, ops Op, recursive bool) error { // Add the given path to be watched. addDir will perform checking for us to // ensure that the path really is a directory. err := fw.addDir(path, pattern, ops) if err != nil { return stackerr.Wrap(err) } if recursive { err = filepath.Walk(path, func(p string, info os.FileInfo, err error) error { if info.IsDir() { // Subdirectories inherit the filename pattern and ops from the parent. if e := fw.addDir(p, pattern, ops); err != nil { return stackerr.Wrap(e) } } return nil }) if err != nil { return err } } return nil }
func (l *login) updatedNetrcContent( e *env, content io.Reader, credentials *credentials, ) ([]byte, error) { tokens, err := netrc.Parse(content) if err != nil { return nil, stackerr.Wrap(err) } server, err := getHostFromURL(e.Server) if err != nil { return nil, err } machine := tokens.FindMachine(server) if machine == nil { machine = tokens.NewMachine(server, "default", credentials.token, "") } else { machine.UpdatePassword(credentials.token) } updatedContent, err := tokens.MarshalText() if err != nil { return nil, stackerr.Wrap(err) } return updatedContent, nil }
func (l *logsCmd) round(e *env, c *context, startTime *parseTime) (*parseTime, error) { v := make(url.Values) v.Set("n", fmt.Sprint(l.num)) v.Set("level", l.level) if startTime != nil { b, err := json.Marshal(startTime) if err != nil { return nil, stackerr.Wrap(err) } v.Set("startTime", string(b)) } u := &url.URL{ Path: "scriptlog", RawQuery: v.Encode(), } var rows []logResponse if _, err := e.ParseAPIClient.Get(u, &rows); err != nil { return nil, stackerr.Wrap(err) } // logs come back in reverse for i := len(rows) - 1; i >= 0; i-- { fmt.Fprintln(e.Out, rows[i].Message) } if len(rows) > 0 { return &rows[0].Timestamp, nil } return startTime, nil }
func (h *functionHooksCmd) functionHooksCreate(e *parsecli.Env, ctx *parsecli.Context) error { params, err := readFunctionParams(e, h.Function) if err != nil { return err } var res functionHook functionsURL, err := url.Parse(defaultFunctionsURL) if err != nil { return stackerr.Wrap(err) } _, err = e.ParseAPIClient.Post(functionsURL, params, &res) if err != nil { return stackerr.Wrap(err) } if res.Warning != "" { fmt.Fprintf(e.Err, "WARNING: %s\n", res.Warning) } fmt.Fprintf(e.Out, "Successfully created a webhook function %q pointing to %q\n", res.FunctionName, res.URL, ) return nil }
func writeProjectConfig(p *projectConfig, path string) error { b, err := json.MarshalIndent(p, "", " ") if err != nil { return stackerr.Wrap(err) } return stackerr.Wrap(ioutil.WriteFile(path, b, 0600)) }
func (s *UserService) create(req *restful.Request, resp *restful.Response) { // TODO (m0sth8): Check permissions raw := &user.User{} if err := req.ReadEntity(raw); err != nil { logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusBadRequest, services.WrongEntityErr) return } mgr := s.Manager() defer mgr.Close() obj, err := mgr.Users.Create(raw) if err != nil { if mgr.IsDup(err) { resp.WriteServiceError( http.StatusConflict, services.NewError(services.CodeDuplicate, "user with this email is existed")) return } logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } resp.WriteHeader(http.StatusCreated) resp.WriteEntity(obj) }
func (h *functionHooksCmd) functionHooksUpdate(e *parsecli.Env, ctx *parsecli.Context) error { params, err := readFunctionParams(e, h.Function) if err != nil { return err } var res functionHook functionsURL, err := url.Parse(path.Join(defaultFunctionsURL, params.FunctionName)) if err != nil { return stackerr.Wrap(err) } _, err = e.ParseAPIClient.Put(functionsURL, &functionHook{URL: params.URL}, &res) if err != nil { return stackerr.Wrap(err) } if res.Warning != "" { fmt.Fprintf(e.Err, "WARNING: %s\n", res.Warning) } fmt.Fprintf(e.Out, "Successfully update the webhook function %q to point to %q\n", res.FunctionName, res.URL, ) return nil }
func (h *triggerHooksCmd) triggerHooksUpdate(e *env, ctx *context) error { params, err := readTriggerParams(e, h.Trigger) if err != nil { return err } var res triggerHook triggersURL, err := url.Parse(path.Join(defaultTriggersURL, params.ClassName, params.TriggerName)) if err != nil { return stackerr.Wrap(err) } _, err = e.ParseAPIClient.Put(triggersURL, &triggerHook{URL: params.URL}, &res) if err != nil { return stackerr.Wrap(err) } if res.Warning != "" { fmt.Fprintf(e.Err, "WARNING: %s\n", res.Warning) } fmt.Fprintf(e.Out, "Successfully update the %q trigger for class %q to point to %q\n", res.TriggerName, res.ClassName, res.URL, ) return nil }
func (h *triggerHooksCmd) triggerHooksDelete(e *env, ctx *context) error { params, err := readTriggerName(e, h.Trigger) if err != nil { return err } triggersURL, err := url.Parse(path.Join(defaultTriggersURL, params.ClassName, params.TriggerName)) if err != nil { return stackerr.Wrap(err) } confirmMessage := fmt.Sprintf("Are you sure you want to delete %q webhook trigger for class: %q (y/n): ", params.TriggerName, params.ClassName, ) var res triggerHook if !h.interactive || getConfirmation(confirmMessage, e) { _, err = e.ParseAPIClient.Put(triggersURL, map[string]interface{}{"__op": "Delete"}, &res) if err != nil { return stackerr.Wrap(err) } fmt.Fprintf(e.Out, "Successfully deleted %q webhook trigger for class %q\n", params.TriggerName, params.ClassName, ) if res.TriggerName != "" && res.ClassName != "" { fmt.Fprintf(e.Out, "%q trigger defined in cloudcode for class %q will be used henceforth\n", res.TriggerName, res.ClassName, ) } } return nil }
// BestEfforDockerClient creates a docker client from one of: // // 1. Environment variables as defined in // https://docs.docker.com/reference/commandline/cli/. Specifically // DOCKER_HOST, DOCKER_TLS_VERIFY & DOCKER_CERT_PATH. // // 2. bootdocker, if darwin. // // 3. /run/docker.sock, if it exists. // // 4. /var/run/docker.sock, if it exists. func BestEffortDockerClient() (*dockerclient.DockerClient, error) { host := os.Getenv("DOCKER_HOST") if host == "" { if runtime.GOOS == "darwin" { return Boot2DockerClient() } socketLocations := []string{"/run/docker.sock", "/var/run/docker.sock"} for _, l := range socketLocations { if _, err := os.Stat(l); err == nil { c, err := dockerclient.NewDockerClient(fmt.Sprintf("unix://%s", l), nil) if err != nil { return nil, stackerr.Wrap(err) } return c, nil } } return nil, stackerr.New("docker not configured") } if os.Getenv("DOCKER_TLS_VERIFY") != "" { return DockerWithTLS(host, os.Getenv("DOCKER_CERT_PATH")) } c, err := dockerclient.NewDockerClient(host, nil) if err != nil { return nil, stackerr.Wrap(err) } return c, nil }
// DockerWithTLS returns a DockerClient with the certs in the specified // directory. The names of the certs are the standard names of "cert.pem", // "key.pem" and "ca.pem". func DockerWithTLS(url, certPath string) (*dockerclient.DockerClient, error) { var tlsConfig *tls.Config clientCert, err := tls.LoadX509KeyPair( filepath.Join(certPath, "cert.pem"), filepath.Join(certPath, "key.pem"), ) if err != nil { return nil, stackerr.Wrap(err) } rootCAs := x509.NewCertPool() caCert, err := ioutil.ReadFile(filepath.Join(certPath, "ca.pem")) if err != nil { return nil, stackerr.Wrap(err) } rootCAs.AppendCertsFromPEM(caCert) tlsConfig = &tls.Config{ Certificates: []tls.Certificate{clientCert}, RootCAs: rootCAs, } client, err := dockerclient.NewDockerClient(url, tlsConfig) if err != nil { return nil, stackerr.Wrap(err) } return client, nil }
// disabled func (s *ScanService) update(req *restful.Request, resp *restful.Response, pl *scan.Scan) { // TODO (m0sth8): Check permissions // TODO (m0sth8): Forbid changes in scan after finished status raw := &scan.Scan{} if err := req.ReadEntity(raw); err != nil { logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusBadRequest, services.WrongEntityErr) return } mgr := s.Manager() defer mgr.Close() raw.Id = pl.Id if err := mgr.Scans.Update(raw); err != nil { if mgr.IsNotFound(err) { resp.WriteErrorString(http.StatusNotFound, "Not found") return } if mgr.IsDup(err) { resp.WriteServiceError( http.StatusConflict, services.NewError(services.CodeDuplicate, "scan with this name and version is existed")) return } logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } resp.WriteHeader(http.StatusOK) resp.WriteEntity(raw) }
// create file with data func (m *FileManager) Create(r io.Reader, metaInfo *file.Meta) (*file.Meta, error) { f, err := m.grid.Create("") // according to gridfs code, the error here is impossible if err != nil { return nil, stackerr.Wrap(err) } size, err := io.Copy(f, r) if err != nil { return nil, stackerr.Wrap(err) } meta := &file.Meta{ Id: file.UniqueFileId(), Size: int(size), ContentType: metaInfo.ContentType, Name: metaInfo.Name, } f.SetId(meta.Id) f.SetMeta(meta) if meta.ContentType != "" { f.SetContentType(meta.ContentType) } if err = f.Close(); err != nil { return nil, stackerr.Wrap(err) } return meta, nil }
func (c *configureCmd) deleteHook( e *env, hooksOps []*hookOperation, fields ...string, ) (bool, []*hookOperation, error) { restOp := strings.ToUpper(fields[0]) if restOp != "DELETE" { return false, nil, stackerr.Wrap(errInvalidFormat) } switch len(fields) { case 2: hooksOps = append(hooksOps, &hookOperation{ method: "DELETE", function: &functionHook{FunctionName: fields[1]}, }) return true, hooksOps, nil case 3: if err := c.checkTriggerName(fields[2]); err != nil { return false, nil, err } hooksOps = append(hooksOps, &hookOperation{ method: "DELETE", trigger: &triggerHook{ClassName: fields[1], TriggerName: fields[2]}, }) return true, hooksOps, nil } return false, nil, stackerr.Wrap(errInvalidFormat) }
func send(s mangos.Socket, obj interface{}) error { data, err := json.Marshal(obj) if err != nil { return stackerr.Wrap(err) } return stackerr.Wrap(s.Send(data)) }
func (h *triggerHooksCmd) triggerHooksCreate(e *env, ctx *context) error { params, err := readTriggerParams(e, h.Trigger) if err != nil { return err } var res triggerHook triggersURL, err := url.Parse(defaultTriggersURL) if err != nil { return stackerr.Wrap(err) } _, err = e.ParseAPIClient.Post(triggersURL, params, &res) if err != nil { return stackerr.Wrap(err) } if res.Warning != "" { fmt.Fprintf(e.Err, "WARNING: %s\n", res.Warning) } fmt.Fprintf(e.Out, "Successfully created a %q trigger for class %q pointing to %q\n", res.TriggerName, res.ClassName, res.URL, ) return nil }
func recv(s mangos.Socket, obj interface{}) error { data, err := s.Recv() if err != nil { return stackerr.Wrap(err) } return stackerr.Wrap(json.Unmarshal(data, obj)) }
func (g *Generator) genForNames(names string) error { certFileName := "cert.pem" keyFileName := "key.pem" rootCert, err := g.loadRootCA() if err != nil { return err } rootKey, err := g.loadRootCAKey() if err != nil { return err } priv, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { return stackerr.Wrap(err) } now := time.Now() template := x509.Certificate{ SerialNumber: new(big.Int).SetInt64(0), Subject: pkix.Name{ CommonName: names, }, NotBefore: now.Add(-5 * time.Minute).UTC(), NotAfter: now.Add(g.MaxAge), SubjectKeyId: []byte{1, 2, 3, 4}, KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, BasicConstraintsValid: true, } derBytes, err := x509.CreateCertificate( rand.Reader, &template, rootCert, &priv.PublicKey, rootKey) if err != nil { return stackerr.Wrap(err) } certOut, err := os.Create(certFileName) if err != nil { return stackerr.Wrap(err) } pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) certOut.Close() log.Print("Written " + certFileName + "\n") keyOut, err := os.OpenFile(keyFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return stackerr.Wrap(err) } pem.Encode(keyOut, &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv), }) keyOut.Close() log.Print("Written " + keyFileName + "\n") return nil return nil }
func (d *downloadCmd) run(e *env, c *context) error { var err error latestRelease := d.release if latestRelease == nil { latestRelease, err = (&deployCmd{}).getPrevDeplInfo(e) if err != nil { return err } if len(latestRelease.Versions.Cloud) == 0 && len(latestRelease.Versions.Public) == 0 { return errNoFiles } } destination := d.destination if destination == "" { destination, err = ioutil.TempDir("", "parse_code_") if err != nil { return stackerr.Wrap(err) } } err = d.download(e, destination, latestRelease) if err != nil { fmt.Fprintln(e.Err, "Failed to download Cloud Code.") return stackerr.Wrap(err) } if !d.force { fmt.Fprintf(e.Out, "Successfully downloaded Cloud Code to %q.\n", destination) return nil } return stackerr.Wrap(d.moveFiles(e, destination, latestRelease)) }
func (i *iosSymbolUploader) convertSymbols(e *env) ([]string, error) { homedir, err := homedir.Dir() if err != nil { return nil, stackerr.Wrap(err) } folderPath := filepath.Join(homedir, ".parse", "CrashReportingSymbols") if err := i.prepareSymbolsFolder(folderPath, e); err != nil { return nil, stackerr.Wrap(err) } conversionTool, err := i.symbolConversionTool(homedir, nil, e) if err != nil { return nil, stackerr.Wrap(err) } cmd := exec.Command(conversionTool, i.Path, folderPath) if out, err := cmd.CombinedOutput(); err != nil { return nil, stackerr.Newf("Symbol conversion failed with:\n%s", string(out)) } filenames, err := readDirNames(folderPath) if err != nil { return nil, stackerr.Wrap(err) } return filenames, nil }
func (h *functionHooksCmd) functionHooksDelete(e *parsecli.Env, ctx *parsecli.Context) error { params, err := readFunctionName(e, h.Function) if err != nil { return err } functionsURL, err := url.Parse(path.Join(defaultFunctionsURL, params.FunctionName)) if err != nil { return stackerr.Wrap(err) } confirmMessage := fmt.Sprintf( "Are you sure you want to delete webhook function: %q (y/n): ", params.FunctionName, ) var res functionHook if !h.interactive || getConfirmation(confirmMessage, e) { _, err = e.ParseAPIClient.Put(functionsURL, map[string]interface{}{"__op": "Delete"}, &res) if err != nil { return stackerr.Wrap(err) } fmt.Fprintf(e.Out, "Successfully deleted webhook function %q\n", params.FunctionName) if res.FunctionName != "" { fmt.Fprintf(e.Out, "Function %q defined in Cloud Code will be used henceforth\n", res.FunctionName) } } return nil }
func (s *PlanService) update(req *restful.Request, resp *restful.Response, pl *plan.Plan) { // TODO (m0sth8): Check permissions raw := &plan.Plan{} if err := req.ReadEntity(raw); err != nil { logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusBadRequest, services.WrongEntityErr) return } mgr := s.Manager() defer mgr.Close() raw.Id = pl.Id if err := mgr.Plans.Update(raw); err != nil { if mgr.IsNotFound(err) { resp.WriteErrorString(http.StatusNotFound, "Not found") return } if mgr.IsDup(err) { resp.WriteServiceError( http.StatusConflict, services.DuplicateErr) return } logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } resp.WriteHeader(http.StatusOK) resp.WriteEntity(raw) }
func (s *MeService) info(req *restful.Request, resp *restful.Response) { mgr := s.Manager() defer mgr.Close() u := filters.GetUser(req) query := manager.Or(fltr.GetQuery(&manager.ProjectFltr{Owner: u.Id, Member: u.Id})) projects, count, err := mgr.Projects.FilterByQuery(query) if err != nil { logrus.Error(stackerr.Wrap(err)) } else { // TODO (m0sth8): create default project when user on create is triggered. // create one default project if count == 0 { p, err := mgr.Projects.CreateDefault(u.Id) if err != nil { logrus.Error(stackerr.Wrap(err)) // It might be possible, that default project is already created // So, client should repeat request resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } else { projects = append(projects, p) } } } info := me.Info{ User: u, Projects: projects, } resp.WriteEntity(info) }
func (d *deployCmd) getPrevDeplInfo(e *parsecli.Env) (*deployInfo, error) { prevDeplInfo := &deployInfo{} if _, err := e.ParseAPIClient.Get(&url.URL{Path: "deploy"}, prevDeplInfo); err != nil { return nil, stackerr.Wrap(err) } legacy := len(prevDeplInfo.Checksums.Cloud) == 0 && len(prevDeplInfo.Checksums.Public) == 0 && len(prevDeplInfo.Versions.Cloud) == 0 && len(prevDeplInfo.Versions.Public) == 0 if legacy { var res struct { ReleaseName string `json:"releaseName,omitempty"` Description string `json:"description,omitempty"` ParseVersion string `json:"parseVersion,omitempty"` Checksums map[string]string `json:"checksums,omitempty"` Versions map[string]string `json:"userFiles,omitempty"` } if _, err := e.ParseAPIClient.Get(&url.URL{Path: "deploy"}, &res); err != nil { return nil, stackerr.Wrap(err) } prevDeplInfo.ReleaseName = res.ReleaseName prevDeplInfo.Description = res.Description prevDeplInfo.ParseVersion = res.ParseVersion prevDeplInfo.Checksums.Cloud = res.Checksums prevDeplInfo.Versions.Cloud = res.Versions } return prevDeplInfo, nil }
func (c *configAPI) GET(w http.ResponseWriter, req *http.Request) error { resp := json.NewEncoder(w) keys := req.URL.Query()["key"] if len(keys) == 0 { w.WriteHeader(http.StatusBadRequest) return stackerr.Wrap(resp.Encode(struct{ Error string }{"Invalid Key"})) } type kvPair struct { Key string `json:"key"` Value []byte `json:"value"` Err string `json:"error,omitempty"` } var kvp []kvPair for _, key := range keys { pair := kvPair{Key: key} val, err := c.dmn.confManager.Get(key) if err != nil { pair.Err = err.Error() } else { pair.Value = val } kvp = append(kvp, pair) } w.WriteHeader(http.StatusOK) return stackerr.Wrap(resp.Encode(kvp)) }
func (r Reader) ReadMessage() ([]byte, error) { c, err := r.b.ReadByte() if err != nil { return nil, stackerr.Wrap(err) } if c != byte(0x0b) { return nil, ErrInvalidHeader(stackerr.Newf("invalid header found; expected 0x0b but got %02x", c)) } d, err := r.b.ReadBytes(byte(0x1c)) if err != nil { return nil, stackerr.Wrap(err) } if len(d) < 2 { return nil, ErrInvalidContent(stackerr.Newf("content including boundary should be at least two bytes long; instead was %d", len(d))) } if d[len(d)-2] != 0x0d { return nil, ErrInvalidBoundary(stackerr.Newf("content should end with 0x0d; instead was %02x", d[len(d)-2])) } t, err := r.b.ReadByte() if err != nil { return nil, stackerr.Wrap(err) } if t != byte(0x0d) { return nil, ErrInvalidTrailer(stackerr.Newf("invalid trailer found; expected 0x0d but got %02x", t)) } return d[0 : len(d)-2], nil }
func (l *login) storeCredentials(e *env, credentials *credentials) error { if l.tokenReader != nil { // tests should not store credentials return nil } homeDir, err := homedir.Dir() if err != nil { return stackerr.Wrap(err) } location := filepath.Join(homeDir, parseNetrc) if err := os.MkdirAll(filepath.Dir(location), 0755); err != nil { return stackerr.Wrap(err) } file, err := os.OpenFile(location, os.O_RDONLY|os.O_CREATE, 0600) if err != nil { return stackerr.Wrap(err) } content, err := l.updatedNetrcContent(e, file, credentials) file, err = os.OpenFile(location, os.O_WRONLY|os.O_TRUNC, 0600) _, err = file.Write(content) return stackerr.Wrap(err) }
func writeLegacyConfigFile(c *legacyConfig, path string) error { b, err := json.MarshalIndent(c, "", " ") if err != nil { return stackerr.Wrap(err) } return stackerr.Wrap(ioutil.WriteFile(path, b, 0600)) }
func (s *ProjectService) update(req *restful.Request, resp *restful.Response, p *project.Project) { raw := &ProjectEntity{} if err := req.ReadEntity(raw); err != nil { logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusBadRequest, services.WrongEntityErr) return } user := filters.GetUser(req) if p.Owner != user.Id { resp.WriteServiceError(http.StatusForbidden, services.AuthForbidErr) return } mgr := s.Manager() defer mgr.Close() if raw.Name != "" { p.Name = raw.Name } if err := mgr.Projects.Update(p); err != nil { if mgr.IsDup(err) { resp.WriteServiceError( http.StatusConflict, services.NewError(services.CodeDuplicate, "project with this name and owner is existed")) return } logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } resp.WriteEntity(p) }
func (h *logsCmd) run(e *parsecli.Env, ctx *parsecli.Context) error { hkConfig, ok := ctx.AppConfig.(*parsecli.HerokuAppConfig) if !ok { return stackerr.New("Unexpected config format") } opts := &heroku.LogSessionCreateOpts{} if h.num == 0 { h.num = 50 } //source := "app" //opts.Source = &source opts.Lines = &h.num opts.Tail = &h.tail session, err := e.HerokuAPIClient.LogSessionCreate( hkConfig.HerokuAppID, opts, ) if err != nil { return stackerr.Wrap(err) } resp, err := http.Get(session.LogplexURL) if err != nil { return stackerr.Wrap(err) } _, err = io.Copy(e.Out, resp.Body) return stackerr.Wrap(err) }