Esempio n. 1
0
func checkfile(w http.ResponseWriter, r *http.Request) {
	req := api.CheckFileRequest{}
	err := readRequest(r, &req)
	if err != nil {
		sendError(w, "Could not parse request")
		return
	}

	accessKey, _ := mailbox.FindKeyByName(req.AccessKeyName)
	if accessKey == nil {
		sendError(w, "Access key invalid")
		return
	}

	path := filepath.Join(filesPath(), req.MD5)

	resp := api.SimpleResponse{}
	if _, err := os.Stat(path); os.IsNotExist(err) {
		resp.Success = false
	} else {
		resp.Success = true
	}
	resp.Sign(accessKey.Name, accessKey.Secret)
	writeResponse(&w, resp)
}
Esempio n. 2
0
// deregister is used by administrative clients to remove mailboxes. It deletes
// the mailbox and all messages from the database.
func deregister(w http.ResponseWriter, r *http.Request) {
	var request api.RegisterRequest
	err := readRequest(r, &request)
	if err != nil {
		sendError(w, err.Error())
		return
	}
	accessKey, _ := mailbox.FindKeyByName(request.AccessKeyName)
	if accessKey == nil {
		sendError(w, "Access key is invalid")
		return
	}
	if !request.Validate(accessKey.Secret) {
		sendError(w, "Could not validate signature")
		return
	}
	if !accessKey.CanAdmin() {
		sendError(w, "Not allowed to deregister mailboxes")
		return
	}
	err = mailbox.Deregister(request.Mailbox)
	if err != nil {
		sendError(w, err.Error())
		return
	}
	resp := api.SimpleResponse{Success: true}
	resp.Sign(accessKey.Name, accessKey.Secret)
	writeResponse(&w, resp)
}
Esempio n. 3
0
func (client *Client) DeregisterMailbox(m string) (*api.SimpleResponse, error) {
	request := &api.RegisterRequest{Mailbox: m}
	request.Sign(client.AccessKeyName, client.AccessKey)
	var response *api.SimpleResponse
	err := client.request("deregister", request, &response)
	if err != nil {
		return nil, err
	}
	if !response.Validate(client.AccessKey) {
		return nil, errors.New("Could not validate signature")
	}
	return response, err
}
Esempio n. 4
0
func (client *Client) Respond(messageId string, msg string, isErr bool) error {
	request := api.ResponseRequest{
		Response: msg,
		Message:  messageId,
		Error:    isErr,
	}
	request.Sign(client.AccessKeyName, client.AccessKey)
	var response api.SimpleResponse
	err := client.request("deploy/respond", request, &response)
	if err != nil {
		return err
	}
	if !response.Validate(client.AccessKey) {
		return errors.New("Could not validate signature")
	}
	return nil
}
Esempio n. 5
0
// deployRespond is used by scripts to "respond" with information from the
// remote system. These responses can then be reported to the admin client that
// deployed the original script.
func deployRespond(w http.ResponseWriter, r *http.Request) {
	var request api.ResponseRequest
	err := readRequest(r, &request)

	if err != nil {
		sendError(w, "Could not parse request")
		return
	}
	accessKey, err := mailbox.FindKeyByName(request.AccessKeyName)
	if accessKey == nil {
		sendError(w, "Access key is not valid")
		return
	}

	if !request.Validate(accessKey.Secret) {
		sendError(w, "Could not validate signature")
		return
	}

	msg, err := mailbox.FindMessage(request.Message)
	if err != nil {
		sendError(w, err.Error())
		return
	}

	if msg == nil {
		sendError(w, "Could not find message "+request.Message)
		return
	}

	mb, err := mailbox.Find(msg.Mailbox)
	if err != nil {
		sendError(w, err.Error())
		return
	}

	if mb == nil {
		sendError(w, "Mailbox not found")
		return
	}

	dep, err := mailbox.FindDeployment(msg.Deployment)
	if err != nil {
		sendError(w, err.Error())
		return
	}
	if dep == nil {
		sendError(w, "Could not find deployment "+msg.Deployment)
		return
	}
	if !accessKey.CanGet(mb) {
		sendError(w, "Not allowed to respond to deploys")
		return
	}
	err = dep.AddResponse(msg.Mailbox, request.Response, request.Error)
	if err != nil {
		sendError(w, err.Error())
		return
	}
	response := api.SimpleResponse{Success: true}
	response.Sign(accessKey.Name, accessKey.Secret)
	log.Infof("Reponse added to %s from %s", dep.Id, msg.Mailbox)
	writeResponse(&w, response)
}
Esempio n. 6
0
func acceptFile(w http.ResponseWriter, r *http.Request) {
	var (
		request = api.UploadFileRequest{}
	)
	defer r.Body.Close()
	// parse form post data
	err := r.ParseMultipartForm(1000000)
	if err != nil {
		sendError(w, err.Error())
		return
	}

	// Get reuqest data and unmarshal
	reqData := r.FormValue("data")
	err = json.Unmarshal([]byte(reqData), &request)
	if err != nil {
		sendError(w, "Could not parse request!")
		return
	}
	accessKey, _ := mailbox.FindKeyByName(request.AccessKeyName)
	if accessKey == nil {
		sendError(w, "Invalid access key")
		return
	}
	if !accessKey.CanAdmin() {
		sendError(w, "Not allowed to upload files")
		return
	}
	if !request.Validate(accessKey.Secret) {
		sendError(w, "Invalid signature")
		return
	}

	// Save the posted file
	file, _, err := r.FormFile("file")
	if file != nil {
		defer file.Close()
	}
	if err != nil {
		sendError(w, err.Error())
		return
	}

	path := filepath.Join(filesPath(), request.MD5)
	out, err := os.Create(path)
	if out != nil {
		defer out.Close()
	}
	if err != nil {
		sendError(w, err.Error())
		return
	}
	_, err = io.Copy(out, file)
	if err != nil {
		sendError(w, err.Error())
		return
	}

	fileHash, err := hashFile(path)
	if err != nil {
		sendError(w, err.Error())
		return
	}

	if request.MD5 != fileHash {
		defer os.Remove(filepath.Join(filesPath(), request.MD5))
		sendError(w, fmt.Sprintf("MD5 missmatch %s != %s", request.MD5, fileHash))
		return
	}

	log.Infof("File uploaded %s", request.MD5)
	response := api.SimpleResponse{Success: true}
	response.Sign(accessKey.Name, accessKey.Secret)
	writeResponse(&w, response)
}