Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid file path while trying to create an action through REST API #4039

Closed
NikosVlagoidis opened this issue Mar 19, 2018 · 22 comments
Closed
Labels

Comments

@NikosVlagoidis
Copy link
Contributor

https://api.stackstorm.com/api/v1/actions/#/actions_controller.post

While I was trying to create a new action which I previous deleted with the REST API so the file is in the filesystem the following error occurred.

Request:

{	
	"name": "test_action",
	"runner_type": "python-script",
	"data_files":[{
		"file_path": "/opt/stackstorm/packs.dev/my_pack/action",
		"content": "test_action.py"
	}]
}

Response:

{
    "faultstring": "Invalid file path: /opt/stackstorm/packs.dev/my_pack/action"
}
@LindsayHill
Copy link
Contributor

I've only seen that used for Mistral & Action Chain workflow definition files, but it looks like it might work for other actions.

At a minimum, one problem you have is that your path looks wrong - I would expect it to end with actions. Looking at how Workflow Designer does it, it uses the same value as the entry point anyway - i.e. it's relative to the action itself. That also seems to use file_path as the full path to the filename - e.g. workflows/test_workflow.yaml, and then provides the content as content body, not file name.

@arm4b arm4b added the API label Mar 19, 2018
@NikosVlagoidis
Copy link
Contributor Author

Do you mean you have to type the actual yaml file in the "content" field?

@Kami
Copy link
Member

Kami commented Mar 20, 2018

Yes, the content attribute needs to contain the actual (text) file content.

And as @LindsayHill has pointed out, the file_path should probably be something like that - /opt/stackstorm/packs.dev/my_pack/actions/test_action.py.

@NikosVlagoidis
Copy link
Contributor Author

But the file already exists on the filesystem! Why can not I use the same?

@Kami
Copy link
Member

Kami commented Mar 20, 2018

If action data files are still on disk and you only deleted / unregistered action metadata from the system, but the action data files and metadata files are still on disk, you should just be able to run st2ctl reload --register-all and action will be re-registered.

If you want to use API for that, I believe you can just leave data_files attribute empty.

@NikosVlagoidis
Copy link
Contributor Author

NikosVlagoidis commented Mar 20, 2018

I have the same problem:

Request:

{	"pack": "my_pack",
	"name": "test_action",
	"runner_type": "python-script",
	"data_files":[{
		"file_path": "/opt/stackstorm/packs.dev/my_pack/actions/test_action.py",
		"content": "import sys"
	}]
}

Response:

{
    "faultstring": "Invalid file path: /opt/stackstorm/packs.dev/my_pack/actions/test_action.py"
}

The following request leads to interval error (notice the missing / in the start of the path):

Requests:

{	"pack": "my_pack",
	"name": "test_action",
	"runner_type": "python-script",
	"data_files":[{
		"file_path": "opt/stackstorm/packs.dev/my_pack/actions/test_action.py",
		"content": "import sys"
	}]
}

Response:

{
"faultstring": "Internal Server Error"
}

@Kami
Copy link
Member

Kami commented Mar 20, 2018

That's likely because the path you specified is outside the base pack directory.

If your pack is named foo and is located in /opt/stackstorm/packs/foo directory, the file_path would need to look like this - /opt/stackstorm/packs/foo/actions/test_action.py or actions/test_action.py.

For security reasons (to prevent directory traversal attacks and allowing user to write in arbitrary directory) you can only write data directly to the pack directory.

I do agree that the error message we throw right now is not very user-friendly so I will update the code to throw a more user-friendly exception which makes it more obvious what is going on.

@NikosVlagoidis
Copy link
Contributor Author

NikosVlagoidis commented Mar 20, 2018

I use the packs.dev for docker to develop my pack. You say I can only write under /opt/stackstorm/packs ?

@NikosVlagoidis
Copy link
Contributor Author

Maybe we should allow packs_base_paths to be written and not only the /packs directory?

@Kami
Copy link
Member

Kami commented Mar 20, 2018

We actually resolve pack path using get_pack_base_path function so you can write under that directory. That function should take packs_base_paths into account (as long as you don't have pack with the same name in two places - in this case, the first entry wins).

@NikosVlagoidis
Copy link
Contributor Author

NikosVlagoidis commented Mar 20, 2018

I think I know what the problem is but I am not sure yet. I have my pack named something like Test Pack and the directory of the pack is testpack. So i get an interval error. When I try to use the pack as Test Pack to create the new action I get that the directory does not exists.

Request:

{	"pack": "Test Pack",
	"name": "test",
	"runner_type": "python-script",
	"data_files":[{
		"file_path": "opt/stackstorm/packs.dev/test/actions/test_action.py",
		"content": "import sys"
	}]
}

Response:

{
    "faultstring": "Content pack \"Test  Pack\" is not found or doesn't contain actions directory. Searched in: /opt/stackstorm/packs,/opt/stackstorm/packs.dev"
}

when i use test it just gets interval error .

@Kami
Copy link
Member

Kami commented Mar 20, 2018

Please gist the output of:

  • ls -la /opt/stackstorm/packs.dev/test
  • cat /opt/stackstorm/packs.dev/test/pack.yaml

It also looks like that the request is invalid, I would try something like this:

{	"pack": "test",
	"name": "Test Pack",
        "ref": "test",
	"runner_type": "python-script",
	"data_files":[{
		"file_path": "test_action.py",
		"content": "import sys"
	}]
}

It's probably related to a space in the pack name - you should use that for the "name" attribute. "pack" and "ref" should not contain spaces and should match the directly name.

@Kami
Copy link
Member

Kami commented Mar 20, 2018

I confirmed the following works fine:

curl -X POST "http://127.0.0.1:9101/v1/actions" -H "Content-Type: application/json" -d '{"data_files": [{"content": "import sys", "file_path": "test_action.py"}], "ref": "test", "name": "Test Pack", "runner_type": "python-script", "pack": "test"}'

In short - in this case, file_path needs to be just a file name relative to the actions/ directory.

@Kami
Copy link
Member

Kami commented Mar 20, 2018

And #4046 fixes the unfriendly error message issue. This should hopefully make it a lot more obvious that the path needs to be relative to the pack actions/ directory.

@Kami
Copy link
Member

Kami commented Mar 20, 2018

77f2a5c should hopefully clear up the documentation.

@NikosVlagoidis
Copy link
Contributor Author

@Kami How can I send a yaml file through post request? I try on a stringified version of a yaml file but without success. Do you have any insight?

@Kami
Copy link
Member

Kami commented Mar 21, 2018

@NikosVlagoidis What kind of error do you get when you trying posting a yaml file?

@NikosVlagoidis
Copy link
Contributor Author

NikosVlagoidis commented Mar 22, 2018

I managed to do it. You need to escape the " " and the write every new line with \n (at least on Postman)

@Kami
Copy link
Member

Kami commented Mar 22, 2018

Glad that you have figured it out.

Another, easier way (if you don't need to do it via the API) would be to simple place those YAML metadata files on disk and run st2ctl reload --register-all.

That's also the recommended way for deploying and managing content.

@NikosVlagoidis
Copy link
Contributor Author

This is the easier way sure, but I would like to implement a web GUI for building action chains (something like the enterprise edition) so I will need to do it via API I guess.

@Kami
Copy link
Member

Kami commented Mar 22, 2018

Yes, in this case that's correct.

@vaibhavkumar049
Copy link

vaibhavkumar049 commented Mar 23, 2022

@Kami can you guide me on how to write a multi-line python code and send it as a request? as in documentation it is mentioned raw file, I am not sure how to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants