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

Validate BaseUrl for orgs #41

Closed
spheromak opened this issue Sep 23, 2014 · 11 comments
Closed

Validate BaseUrl for orgs #41

spheromak opened this issue Sep 23, 2014 · 11 comments

Comments

@spheromak
Copy link
Member

When invalid org is sent to chef-server bad things happen. We should just validate the BaseUrl at minimum has a trailing slash.

@sethdmoore
Copy link

I've been having trouble with your chef-client lib and hosted chef organizations.

Specifically, I've noticed that when I assign the client from the package function and call a method, it replaces portions of my BaseUrl route and hosted chef-api returns an error. I have to add a fake 'endpoint' to my BaseUrl or else I get 403'd.

Official Chef-API Docs say that in order to interact with their API, you need to prefix the endpoint with organizations/[orgname]/[function]
Like

GET https://api.opscode.com/organizations/mycompany/cookbooks

Here is my workaround, adding the "/foo" portion to get around the limitation.

func Main() {
    key := readKey()
    url, org := setupChef()
    baseurl := url + "/organizations/" + org + "/foo"
    fmt.Println(baseurl)
    client, err := chef.NewClient(&chef.Config{
        Name:    "sethmoore",
        Key:     string(key),
        BaseURL: baseurl,
    })

    cookList, err := client.Cookbooks.List()
    if err != nil {
        fmt.Println("ERROR:", err)
    }

}

Without the "/foo" appended to BaseUrl, I get

ERROR: GET https://api.opscode.com/organizations/cookbooks: 403

I have yet to figure out how to workaround searching.

@spheromak
Copy link
Member Author

@sethdmoore thanks for the detailed report. I haven't been actively working with a chef-server, but I always thought what you were trying would 'just work'. I think https://github.com/go-chef/chef/blob/master/http.go#L171 this line may be the culprit. If not it is probably in NewRequest.

Search in itself is a wild beast. No surprise it didn't behave.

@bigkraig
Copy link
Member

bigkraig commented Jun 4, 2015

I've seen this problem too, you can remove the 'foo' part, you just need a trailing /

baseurl := url + "/organizations/" + org + "/"

@spheromak
Copy link
Member Author

@bigkraig is that it ? I can just detect / correct this or document it if so.

@bigkraig
Copy link
Member

bigkraig commented Jun 4, 2015

It "worked for me" :)

@marklap
Copy link

marklap commented Jul 13, 2015

Thanks! This saved me from pulling my hair out. The trailing slash at the end of Client.BaseURL also worked for me. Otherwise my workaround was this ugly thing:

query, err := client.Search.NewQuery("../../../organizations/myorg/search/node", "name:*")

So my call to NewClient now looks similar to this (note the trailing slash at the end of BaseURL):

    client, err := chef.NewClient(&chef.Config{
        Name:    "ChefClient",
        Key:     string(chef_key),
        BaseURL: "https://my.chef.server.com/organizations/myorg/",
    })

@sean-horn
Copy link

Tested on Chef Server 12.3.1 with the examples/search.go and examples/cookbooks.go examples from 849dab5

Works just fine with the following client config where the baseURL trailing slash is present after the orgname, SSL verification is skipped, and the client name matches the valid user key.

func main() {
        // read a client key
        key, err := ioutil.ReadFile("rainbowdash.pem")
        if err != nil {
                fmt.Println("Couldn't read key.pem:", err)
                os.Exit(1)
        }

        // build a client
        client, err := chef.NewClient(&chef.Config{
                Name: "rainbowdash",
                Key:  string(key),
                // goiardi is on port 4545 by default. chef-zero is 8889
                BaseURL: "https://hr.puffin.stuff.lxc/organizations/ponyville/",
                SkipSSL: true,

@spheromak
Copy link
Member Author

Thanks, I think we should probably validate that then.

@spheromak
Copy link
Member Author

This is also related to #54

@MarkGibbons
Copy link
Member

Initial plans for specifying and using the base url.

For global endpoints with a base specified.

For organization endpoints

Need to check for odd cases and figure out what it does now.

@MarkGibbons
Copy link
Member

Fixed by #209.

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

No branches or pull requests

6 participants