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

Add "file" type for inventory #436

Merged
merged 10 commits into from Mar 8, 2018
Merged

Add "file" type for inventory #436

merged 10 commits into from Mar 8, 2018

Conversation

fiftin
Copy link
Collaborator

@fiftin fiftin commented Oct 26, 2017

No description provided.

@fiftin fiftin changed the title Add "file" type for enventory Add "file" type for inventory Feb 16, 2018
@strangeman
Copy link
Contributor

Is this necessary? In my opinion, solutions, that simply duplicates existing ansible-playbook options is not good, they make a huge mess with Extra CLI options.
We always can pass an inventory file via Extra CLI options, no need to make the separate field on a form. This is the way to hell because ansible-playbook has around 30 various flags and we cannot add them all, it will make form completely unusable.

@fiftin
Copy link
Collaborator Author

fiftin commented Feb 23, 2018

I think this is most important flag. I use it each time when I use ansible-playbook. Adding it as Extra CLI options absolutely unusable. I sure this option will be popular.

@twhiston
Copy link
Contributor

twhiston commented Feb 23, 2018

I think the current 'accepted' way of doing this, a blank inventory and a cli command, seems super hacky and unintuitive. What I like about adding a file field to this is that essentially then you can upload ec2.py (or whatever) in your repo and pass it as the inventory flag, so this effectively makes it possible to cover all dynamic inventory use cases, without any major changes (or am i wrong about this?)
I would suggest though that we remove all the other option in the list as they are disabled anyway and this removes the need for them

@strangeman
Copy link
Contributor

Okay, your arguments are persuasive. :)

@twhiston
Copy link
Contributor

@fiftin can you remove the other inventory type options in the be and fe so that we are left with just static and file?

@fiftin
Copy link
Collaborator Author

fiftin commented Mar 8, 2018

Done

@fiftin
Copy link
Collaborator Author

fiftin commented Mar 8, 2018

I found security issue in my solution. User can enter something like this ../../file or C:\file. I want to add inventory path validation but not sure how to do it to support Windows.

What do you this about this:

func isValidInventoryPath(path string) bool {
	if currentPath, err := filepath.Abs("./"); err != nil {
		return false
	} else if absPath, err := filepath.Abs(path); err != nil {
		return false
	} else if relPath, err := filepath.Rel(currentPath, absPath); err != nil {
		return false
	} else {
		return !strings.HasPrefix(relPath, "..")
	}
}

@@ -123,6 +125,19 @@ func AddInventory(w http.ResponseWriter, r *http.Request) {
mulekick.WriteJSON(w, http.StatusCreated, inv)
}

func isValidInventoryPath(path string) bool {
if currentPath, err := filepath.Abs("./"); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you could use os.Getwd() here instead
https://golang.org/src/os/getwd.go

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also you might need to look at using https://golang.org/pkg/os/#pkg-constants for the filepath separator to make things cross platform compatible if getwd is not suitable

@fiftin
Copy link
Collaborator Author

fiftin commented Mar 8, 2018

Added gopkg.in/check.v1

@twhiston
Copy link
Contributor

twhiston commented Mar 8, 2018

I would prefer if you did not use that test package, it is not really necessary to add it to do the checks you are performing here

@fiftin
Copy link
Collaborator Author

fiftin commented Mar 8, 2018

What test do you mean?

@fiftin
Copy link
Collaborator Author

fiftin commented Mar 8, 2018

Why not use unit test framework?

@twhiston
Copy link
Contributor

twhiston commented Mar 8, 2018

I dont honestly think that you need or want unit test frameworks in go in 99% of cases.

In your test case you can just say

if !projects.IsValidInventoryPath("inventories/test"){
  t.Fatal(" path with attibute x should be valid")
}

That is all you need!

Something so simple should not be replaced by a third party dependency

@fiftin
Copy link
Collaborator Author

fiftin commented Mar 8, 2018

Where I should place this test?

@twhiston
Copy link
Contributor

twhiston commented Mar 8, 2018

So i would replace your current tests with a simple test file such as the util_test.go implementation. It should be in the api/projects dir and should be called inventory_test.go
See https://golang.org/pkg/testing/ for the basics around where tests should be and how they should be named

It would basically look like this:

package projects

import (
	"testing"
)

func TestIsValidInventoryPath(t *testing.T) {

         if !projects.IsValidInventoryPath("inventories/test"){
             t.Fatal(" a path below the cwd should be valid")
         }

...

and so on inside the function for all your test cases

@twhiston
Copy link
Contributor

twhiston commented Mar 8, 2018

I also use goland to run my unit tests (and even better code coverage :D), but you dont need anything special for this or any external libraries. IMO we should be implementing tests in as native a manner as possible, and that means doing it in the way the language specification describes, putting them in the same package etc...... as per the above link to the testing package.

@fiftin
Copy link
Collaborator Author

fiftin commented Mar 8, 2018

Done

@@ -21,7 +22,7 @@ func TestIsValidInventoryPath(t *testing.T) {
t.Fatal(" a path out of the cwd should be invalid")
}

if IsValidInventoryPath("c:\\test\\inventory") {
if runtime.GOOS == "windows" && IsValidInventoryPath("c:\\test\\inventory") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this will ever get run, as it's impossible to test in windows on CircleCI 😬
But it's good to have as a test anyway!

@twhiston
Copy link
Contributor

twhiston commented Mar 8, 2018

Just tested this and it is working great with files for inventories (including python scripts) so we now have dynamic inventory support thanks to your efforts. Great work!

@twhiston twhiston merged commit 88647c8 into semaphoreui:develop Mar 8, 2018
@fiftin fiftin deleted the file_inventory branch March 9, 2018 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants