summaryrefslogtreecommitdiff
path: root/gopherschwift/package.go
blob: b0d09b8a5685db920b71f0d3c9bfcdc1ea8afc4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// SPDX-FileCopyrightText: 2018 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

/*
Package gopherschwift contains a Gophercloud backend for Schwift.

If your application uses Gophercloud (https://github.com/gophercloud/gophercloud/v2),
you can use the Wrap() function in this package as an entrypoint to Schwift.
A schwift.Account created this way will re-use Gophercloud's authentication code,
so you only need to obtain a client token once using Gophercloud. For example:

	import (
		"github.com/gophercloud/gophercloud/v2/openstack"
		"github.com/gophercloud/utils/v2/openstack/clientconfig"
		"go.xyrillian.de/schwift/v2/gopherschwift"
	)

	provider, err := clientconfig.AuthenticatedClient(nil)
	client, err := openstack.NewObjectStorageV1(provider, gophercloud.EndpointOpts{})
	account, err := gopherschwift.Wrap(client)

Using this schwift.Account instance, you have access to all of schwift's API.
Refer to the documentation in the parent package for details.
*/
package gopherschwift

import (
	"io"
	"net/http"

	"github.com/gophercloud/gophercloud/v2"

	"go.xyrillian.de/schwift/v2"
)

// Options contains additional options that can be passed to Wrap().
type Options struct {
	// If set, this User-Agent will be reported in HTTP requests instead of
	// schwift.DefaultUserAgent.
	UserAgent string
}

// Wrap creates a schwift.Account that uses the given service client as its
// backend. The service client must refer to a Swift endpoint, i.e. it should
// have been created by openstack.NewObjectStorageV1().
func Wrap(client *gophercloud.ServiceClient, opts *Options) (*schwift.Account, error) {
	b := &backend{
		c:         client,
		userAgent: schwift.DefaultUserAgent,
	}
	if opts != nil && opts.UserAgent != "" {
		b.userAgent = opts.UserAgent
	}
	return schwift.InitializeAccount(b)
}

type backend struct {
	c         *gophercloud.ServiceClient
	userAgent string
}

// EndpointURL implements the [schwift.Backend] interface.
func (g *backend) EndpointURL() string {
	return g.c.Endpoint
}

// Clone implements the [schwift.Backend] interface.
func (g *backend) Clone(newEndpointURL string) schwift.Backend {
	clonedClient := *g.c
	clonedClient.Endpoint = newEndpointURL
	return &backend{
		c:         &clonedClient,
		userAgent: g.userAgent,
	}
}

// Do implements the [schwift.Backend] interface.
func (g *backend) Do(req *http.Request) (*http.Response, error) {
	return g.do(req, false)
}

func (g *backend) do(req *http.Request, afterReauth bool) (*http.Response, error) {
	provider := g.c.ProviderClient

	for key, value := range provider.AuthenticatedHeaders() {
		req.Header.Set(key, value)
	}
	req.Header.Set("User-Agent", g.userAgent)

	resp, err := provider.HTTPClient.Do(req)
	if err != nil {
		return nil, err
	}

	// detect expired token
	if resp.StatusCode == http.StatusUnauthorized && !afterReauth {
		_, err := io.Copy(io.Discard, resp.Body)
		if err != nil {
			return nil, err
		}
		err = resp.Body.Close()
		if err != nil {
			return nil, err
		}
		err = provider.Reauthenticate(req.Context(), resp.Request.Header.Get("X-Auth-Token"))
		if err != nil {
			return nil, err
		}

		// Swift is stupid: Even though we send `Expect: 100-continue`, it doesn't
		// help. Swift will right away answer `100 Continue` and ONLY THEN actually
		// check the token (at least in our prod setup).
		//
		// To increase the chance that this does not completely break this request,
		// reset the reader if it implements Seek().
		if seekableReqBody, ok := req.Body.(io.Seeker); ok {
			_, err := seekableReqBody.Seek(0, io.SeekStart)
			if err != nil {
				return nil, err
			}
		}

		// restart request with new token
		return g.do(req, true)
	}

	return resp, nil
}