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

403 Target Service not allowed #869

Closed
kashingonline opened this issue Jan 17, 2024 · 2 comments
Closed

403 Target Service not allowed #869

kashingonline opened this issue Jan 17, 2024 · 2 comments
Labels
waiting Waiting for additional information

Comments

@kashingonline
Copy link

kashingonline commented Jan 17, 2024

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:https://%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:https://%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.
@flimzy
Copy link
Member

flimzy commented Jan 25, 2024

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 flimzy added the waiting Waiting for additional information label Jan 25, 2024
@flimzy
Copy link
Member

flimzy commented Jul 19, 2024

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 flimzy closed this as completed Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting Waiting for additional information
Projects
None yet
Development

No branches or pull requests

2 participants