Coder Social home page Coder Social logo

go-artifactory's Introduction

go-artifactory

go-artifactory is a Go client library for accessing the Artifactory API

Build Status

V2 Breaking Changes

Due to the release of the new artifactory api this library has been significantly reworked. The import path has changed to github.com/atlassian/go-artifactory/pkg/artifactory to github.com/atlassian/go-artifactory/artifactory.

Services can now be accessed by including their API version i.e rt.V1.Security instead of rt.Security

Requirements

  • Go version 1.11+
  • Go mod is used for dependency management

Usage

import "github.com/atlassian/go-artifactory/artifactory"

Construct a new Artifactory client, then use the various services on the client to access different parts of the Artifactory API. For example:

client := artifactory.NewClient(client, nil)

// list all repositories
repos, resp, err := client.V1.Repositories.List(context.Background(), nil)

Some API methods have optional parameters that can be passed. For example:

client := artifactroy.NewClient(client, nil)

// list all public local repositories
opt := &artifactory.RepositoryListOptions{Type: "local"}
client.V1.Repositories.ListRepositories(ctx, opt)

The services of a client divide the API into logical chunks and correspond to the structure of the Artifactory API documentation at https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API.

NOTE: Using the context package, one can easily pass cancelation signals and deadlines to various services of the client for handling a request. In case there is no context available, then context.Background() can be used as a starting point.

Authentication

The go-artifactory library does not directly handle authentication. Instead, when creating a new client, pass an http.Client that can handle authentication for you.

For API methods that require HTTP Basic Authentication, use the BasicAuthTransport or TokenTransport

package main

import (
	"github.com/atlassian/go-artifactory/pkg/artifactory"
	"fmt"
	"context"
)

func main() {
	tp := artifactory.BasicAuthTransport{
		Username: "<YOUR_USERNAME>",
		Password: "<YOUR_PASSWORD>",
	}
	
	client, err := artifactory.NewClient("https://localhost/artifactory", tp.Client())
	if err != nil {
		fmt.Println(err.Error())
	}

	repos, resp, err := client.V1.Repositories.ListRepositories(context.Background(), nil)
}

Creating and Updating Resources

All structs for GitHub resources use pointer values for all non-repeated fields. This allows distinguishing between unset fields and those set to a zero-value. Helper functions have been provided to easily create these pointers for string, bool, and int values. For example:

    // create a new local repository named "lib-releases"
    repo := artifactory.LocalRepository{
		Key:             artifactory.String("lib-releases"),
		RClass:          artifactory.String("local"),
		PackageType:     artifactory.String("maven"),
		HandleSnapshots: artifactory.Bool(false);
	}

	client.V1.Repositories.CreateLocal(context.Background(), &repo)

Users who have worked with protocol buffers should find this pattern familiar.

Roadmap

This library is being initially developed for an internal application at Atlassian, so API methods will likely be implemented in the order that they are needed by that application. Eventually, it would be ideal to cover the entire Artifactory API, so contributions are of course always welcome. The calling pattern is pretty well established, so adding new methods is relatively straightforward.

Versioning

In general, go-artifactory follows semver as closely as we can for tagging releases of the package. For self-contained libraries, the application of semantic versioning is relatively straightforward and generally understood. But because go-artifactory is a client library for the Artifactory API we've adopted the following versioning policy:

  • We increment the major version with any incompatible change to functionality, including changes to the exported Go API surface or behavior of the API.
  • We increment the minor version with any backwards-compatible changes to functionality.
  • We increment the patch version with any backwards-compatible bug fixes.

Generally methods will be annotated with a since version.

Reporting issues

We believe in open contributions and the power of a strong development community. Please read our Contributing guidelines on how to contribute back and report issues to go-artifactory.

Contributors

Pull requests, issues and comments are welcomed. For pull requests:

  • Add tests for new features and bug fixes
  • Follow the existing style
  • Separate unrelated changes into multiple pull requests
  • Read Contributing guidelines for more details

See the existing issues for things to start contributing.

For bigger changes, make sure you start a discussion first by creating an issue and explaining the intended change.

Atlassian requires contributors to sign a Contributor License Agreement, known as a CLA. This serves as a record stating that the contributor is entitled to contribute the code/documentation/translation to the project and is willing to have it used in distributions and derivative works (or is willing to transfer ownership).

Prior to accepting your contributions we ask that you please follow the appropriate link below to digitally sign the CLA. The Corporate CLA is for those who are contributing as a member of an organization and the individual CLA is for those contributing as an individual.

License

Copyright (c) 2017 Atlassian and others. Apache 2.0 licensed, see LICENSE file.

go-artifactory's People

Contributors

bodgit avatar dillon-giacoppo avatar jamestoyer avatar kierranm avatar volkc-basf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-artifactory's Issues

panic on CreateToken

Describe the bug
calling CreateToken will panic because of body is nil. The test is wrong, it should return if body is nil, not if it is not nil.
https://github.com/atlassian/go-artifactory/blob/v2/artifactory/client/client.go#L94

To Reproduce

tokenOpts := v1.AccessTokenOptions{
		Username: artifactory.String("admin"),
		Scope:    artifactory.String("member-of-groups:*"),
}
client.V1.Security.CreateToken(context.Background(), &tokenOpts)

it will panic:
panic: runtime error: invalid memory address or nil pointer dereference

Expected behavior
return the token

Log Output

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x11e2d32]

goroutine 1 [running]:
strings.(*Reader).Len(...)
        /usr/local/Cellar/go/1.11.4/libexec/src/strings/reader.go:25
net/http.NewRequest(0x12acdd7, 0x4, 0xc000110420, 0x48, 0x12f19c0, 0x0, 0x0, 0xd0, 0x6)
        /usr/local/Cellar/go/1.11.4/libexec/src/net/http/request.go:839 +0x282
github.com/atlassian/go-artifactory/artifactory/client.(*Client).NewRequest(0xc00000c3c0, 0x12acdd7, 0x4, 0x12b024d, 0x13, 0x12f19c0, 0x0, 0xd0, 0x6, 0x0)
        /Users/sam/go/src/github.com/atlassian/go-artifactory/artifactory/client/client.go:69 +0x11c
github.com/atlassian/go-artifactory/artifactory/client.(*Client).NewURLEncodedRequest(0xc00000c3c0, 0x12acdd7, 0x4, 0x12b024d, 0x13, 0x1240ce0, 0xc00010c3f0, 0x1, 0x1, 0x6)
        /Users/sam/go/src/github.com/atlassian/go-artifactory/artifactory/client/client.go:128 +0xa4
github.com/atlassian/go-artifactory/artifactory/v1.(*SecurityService).CreateToken(0xc00009cd20, 0x12f3d80, 0xc0000180b8, 0xc00010c3f0, 0x0, 0x0, 0xc0001122c0, 0x0)
        /Users/sam/go/src/github.com/atlassian/go-artifactory/artifactory/v1/security.go:908 +0x75
exit status 2

Desktop (please complete the following information):

  • OS: MacOS

Additional context
The bug is probably here:
https://github.com/atlassian/go-artifactory/blob/v2/artifactory/client/client.go#L94
should be

	if body == nil {

Crash when using v1.Security.AddCertificate()

Describe the bug
I'm trying to use v1.Security.AddCertificate() as part of atlassian/terraform-provider-artifactory#38 and I'm either doing something wrong or there's a bug here.

To Reproduce
My provider code looks like this:

func resourceCertificateUpdate(d *schema.ResourceData, m interface{}) error {
        c := m.(*artifactory.Artifactory)

        pem, err := os.Open(d.Get("path").(string))
        if err != nil {
                return err
        }
        defer pem.Close()

        _, _, err = c.V1.Security.AddCertificate(context.Background(), d.Id(), pem)
        if err != nil {
                return err
        }

        return resourceCertificateRead(d, m)
}

v1.Security.AddCertificate() wants the certificate passed as an *os.File hence I assume I just open the file and pass it in like this? The file is open successfully and if I read it, there's clearly PEM data there.

Expected behavior
I would expect the call to succeed.

Log Output
There is a lot of Terraform log, but the top-most bits are:

2019-05-09T16:28:39.942+0100 [DEBUG] plugin.terraform-provider-artifactory: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xd0f3ca]
2019-05-09T16:28:39.942+0100 [DEBUG] plugin.terraform-provider-artifactory:
2019-05-09T16:28:39.942+0100 [DEBUG] plugin.terraform-provider-artifactory: goroutine 58 [running]:
2019-05-09T16:28:39.942+0100 [DEBUG] plugin.terraform-provider-artifactory: github.com/atlassian/go-artifactory/artifactory/transport.(*BasicAuth).RoundTrip(0xc00013d2c0, 0xc000458600, 0xc00013d2c0, 0x0, 0x0)
2019-05-09T16:28:39.942+0100 [DEBUG] plugin.terraform-provider-artifactory:     .../work/src/github.com/atlassian/go-artifactory/artifactory/transport/basicauth.go:44 +0x19a

It's crashing on this line:

reader, _ := req.GetBody()

req.GetBody() is nil. I added some debug to print the request before it gets sent, and this is what it looks like:

&{POST https://artifactory.example.com/artifactory/api/system/security/certificates/test HTTP/1.1 1 1 map[Accept:[application/json] Content-Type:[text/plain] User-Agent:[go-artifactory]] 0xc0000ad2d0 <nil> 0 [] false artifactory.example.com map[] map[] <nil> map[]   <nil> <nil> <nil> <nil>}

GetBody() is the <nil> following the 0xc0000ad2d0 address following the http.Request struct.

Desktop (please complete the following information):

  • OS: Linux, go version go1.12.4 linux/amd64

Additional context
Incidentally, v1.Security.AddCertificate() calls client.NewClient() which wants an io.Reader instead of a *os.File; using the former everywhere instead would mean you could feed in a stream of bytes for the PEM from a string rather than always having to reference an existing file on the disk, assuming I've got the API right.

Getting error when calling FileInfo

trying to use the FileInfo function and i'm getting error

[{Status:406 Message:Resource produces application/vnd.org.jfrog.artifactory.storage.FolderInfo+json but client only accepts [application/vnd.org.jfrog.artifactory.storage.FileInfo+json]}]

we are using Artifactory HA version 7.2.1

and one have an idea ?

dep: unable to solve the dependency graph / mod: error loading module requirements

Describe the bug
When I try to include this library, dep errors because of malformed code

To Reproduce

  1. create new go file
  2. import "github.com/atlassian/go-artifactory/artifactory"
  3. run dep init

Expected behavior
to resolve this library

Log Output

dep init
init failed: unable to solve the dependency graph: Solving failure: No versions of github.com/atlassian/go-artifactory met constraints:
	v2.2.0: "github.com/atlassian/go-artifactory/artifactory" imports "github.com/atlassian/go-artifactory/v2/artifactory/client", which contains malformed code: no package exists at "github.com/atlassian/go-artifactory/v2/artifactory/client"
	v2.1.1: "github.com/atlassian/go-artifactory/artifactory" imports "github.com/atlassian/go-artifactory/v2/artifactory/client", which contains malformed code: unknown error for "github.com/atlassian/go-artifactory/v2/artifactory/client", if you get this error see https://github.com/golang/dep/issues/351
	v2.1.0: "github.com/atlassian/go-artifactory/artifactory" imports "github.com/atlassian/go-artifactory/v2/artifactory/client", which contains malformed code: unknown error for "github.com/atlassian/go-artifactory/v2/artifactory/client", if you get this error see https://github.com/golang/dep/issues/351
	v2.0.0: "github.com/atlassian/go-artifactory/artifactory" imports "github.com/atlassian/go-artifactory/v2/artifactory/client", which contains malformed code: unknown error for "github.com/atlassian/go-artifactory/v2/artifactory/client", if you get this error see https://github.com/golang/dep/issues/351
	v1.2.0: Could not introduce github.com/atlassian/[email protected], as its subpackage github.com/atlassian/go-artifactory/artifactory is missing. (Package is required by (root).)
	v1.1.0: Could not introduce github.com/atlassian/[email protected], as its subpackage github.com/atlassian/go-artifactory/artifactory is missing. (Package is required by (root).)
	v1.0.1: Could not introduce github.com/atlassian/[email protected], as its subpackage github.com/atlassian/go-artifactory/artifactory is missing. (Package is required by (root).)
	v1.0.0: Could not introduce github.com/atlassian/[email protected], as its subpackage github.com/atlassian/go-artifactory/artifactory is missing. (Package is required by (root).)
	v2.0.0-beta: "github.com/atlassian/go-artifactory/artifactory" imports "github.com/atlassian/go-artifactory/v2/artifactory/v2", which contains malformed code: no package exists at "github.com/atlassian/go-artifactory/v2/artifactory/v2"
	v2.0.0-alpha: Could not introduce github.com/atlassian/[email protected], as its subpackage github.com/atlassian/go-artifactory/artifactory is missing. (Package is required by (root).)
	v2: "github.com/atlassian/go-artifactory/artifactory" imports "github.com/atlassian/go-artifactory/v2/artifactory/client", which contains malformed code: unknown error for "github.com/atlassian/go-artifactory/v2/artifactory/client", if you get this error see https://github.com/golang/dep/issues/351
	master: Could not introduce github.com/atlassian/go-artifactory@master, as its subpackage github.com/atlassian/go-artifactory/artifactory is missing. (Package is required by (root).)

Desktop (please complete the following information):

  • OS: OSX

http.Response.Body() closed in Do() regardless of value of `v`

Describe the bug
In attempting to read the response body from the return http.Response from client.Do() when v is nil, the following error is received:

http2: response body closed

To Reproduce
Steps to reproduce the behavior:

  1. Create a new client (artifactory.NewClient("artifactory url", nil))
  2. Create a new request (client.NewRequest(http.MethodGet, "path", nil))
  3. Execute the request (client.Do(ctx, request, nil))

Expected behavior
A readable response.Body that has yet to be closed.

Add priorityResolution field for local/remote repositories

Is your feature request related to a problem? Please describe.
The new priorityResolution field, for local and remote repositories, is currently not supported. See https://www.jfrog.com/confluence/display/JFROG/Repository+Configuration+JSON for api reference.

Describe the solution you'd like
I would like priorityResolution to be added to https://github.com/atlassian/go-artifactory/blob/master/artifactory/v1/repositories.go#L48 and https://github.com/atlassian/go-artifactory/blob/master/artifactory/v1/repositories.go#L162

Describe alternatives you've considered
I would not like to fork the repo

Additional context
Planning to use the field in https://github.com/jfrog/terraform-provider-artifactory

Add the resolveDockerTagsByTimestamp argument to virtual repository model

Is your feature request related to a problem? Please describe.
Add the resolveDockerTagsByTimestamp argument to virtual repository model.

As of Artifactory version 6.12 it now supports the ability to resolve images with the same docker tags by timestamp. See the release notes here.

Describe the solution you'd like
I would like to be able to set the resolveDockerTagsByTimestamp flag on the virtual repository model and use it to create a repository with that setting and update the setting.

Describe alternatives you've considered
None

Additional context

Support for TLS authentication

Often times, corporate environments rely on PKI for authentication. It would be nice if this module also supported client-side TLS - using client keypair and custom CA certificates.

This should be straightforward with how nice the library is structured -- just another transport implementation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.