kivik icon indicating copy to clipboard operation
kivik copied to clipboard

403 Target Service not allowed

Open kashingonline opened this issue 1 year ago • 1 comments

Bug Report Checklist (remove this template if submitting a feature request)

  1. Which version of Kivik are you using? Please state a version number, or master. I am using version 3

  2. Which version of Go are you using? (Output of go version) -- Kivik 3.x supports Go 1.13 and later. Kivik 4.x requires Go 1.17 or later.

go version go1.21.5 linux/amd64

  1. What did you do? (Include your code if possible)
package database

import (
	"fmt"
	"net/http"
	"my-app/pkg/config"

	"github.com/go-kivik/kivik/v3"
	"github.com/go-resty/resty/v2"
)

var cfg = config.New()
var DB *KivikDB

type KivikDB struct {
    Client *kivik.Client
    DBName string
}

func init() {
    DB = New()
}

func (db *KivikDB) GetDBVersion() (string, error) {
    client := resty.New()

    // Disable proxy use by setting a custom transport
    client.SetTransport(&http.Transport{
        Proxy: nil,
    })

    // Construct the server URL
    serverURL := fmt.Sprintf("%s://%s:%s", cfg.DB.Protocol, cfg.DB.Host, cfg.DB.Port)

    // Include authentication if needed
    if cfg.DB.Username != "" && cfg.DB.Password != "" {
        client.SetBasicAuth(cfg.DB.Username, cfg.DB.Password)
    }

    // Create an anonymous struct to hold the response
    var result struct {
        Version string `json:"version"`
    }

    // Perform the GET request
    resp, err := client.R().
        SetResult(&result).
        Get(serverURL)
    if err != nil {
        return "", err
    }

    // Check for a successful response
    if resp.StatusCode() != http.StatusOK {
        return "", fmt.Errorf("failed to get version, status code: %d", resp.StatusCode())
    }

    return result.Version, nil
}

func New() *KivikDB {
    log.Info("Initializing new KivikDB client...")

    // URL encode the username and password
    //encodedUsername := url.QueryEscape(cfg.DB.Username)
    //encodedPassword := url.QueryEscape(cfg.DB.Password)

    // Construct the connection string with the encoded values
    connectionString := fmt.Sprintf("%s://%s:%s@%s:%s", cfg.DB.Protocol, cfg.DB.Username, cfg.DB.Password, cfg.DB.Host, cfg.DB.Port)

    client, err := kivik.New("couch", connectionString)
    if err != nil {
        log.Fatalf("Error creating Kivik client: %v", err)
    }

    db := &KivikDB{
        Client: client,
        DBName: cfg.DB.Database,
    }

    version, err := db.GetDBVersion()
    if err != nil {
        log.Fatalf("Error getting DB version: %v", err)
    }
    log.Infof("Connected to CouchDB version: %s", version)

    log.Info("KivikDB successfully initialized.")

    return db
}

I can connect and see the version: 3:29:12.135680 setup.go:12: [Info] Initializing new KivikDB client... 3:29:12.136081 WARN RESTY Using Basic Auth in HTTP mode is not secure, use HTTPS 3:29:12.145700 setup.go:35: [Info] Connected to CouchDB version: 3.3.3 3:29:12.145914 setup.go:37: [Info] KivikDB successfully initialized.

The problem comes in when I do a find:

// Perform the query
rows, err := database.DB.Client.DB(ctx, database.DB.DBName).Find(ctx, query)
if err != nil {
    log.Printf("Error performing query: %v\n", err)
    return nil, err
}

*chttp.HTTPError {Response: *net/http.Response {Status: "403 Target service not allowed", StatusCode: 403, Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1, Header: net/http.Header [...], Body: io.ReadCloser(*net/http.bodyEOFSignal) ..., ContentLength: 2524, TransferEncoding: []string len: 0, cap: 0, nil, Close: false, Uncompressed: false, Trailer: net/http.Header nil, Request: ("net/http.Request")(0xc0002ae400), TLS: *crypto/tls.ConnectionState nil}, Reason: "", exitStatus: 22}

I can do a curl and that works fine.

  1. What did you expect to see?

I hope to see my data and 200 ok

  1. What did you see instead? I saw 403 Target service not allowed. weird.

kashingonline avatar Jan 17 '24 13:01 kashingonline

Do you have some sort of proxy in your network?

403 Target Service not allowed is not an error generated either by Kivik,or by CouchDB.

flimzy avatar Jan 25 '24 21:01 flimzy

It's been nearly 6 months, without any follow-up. Please re-open if this is still an issue, and you can provide additional information.

flimzy avatar Jul 19 '24 15:07 flimzy