1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright ownership.
5+ * The ASF licenses this file to You under the Apache License, Version 2.0
6+ * (the "License"); you may not use this file except in compliance with
7+ * the License. You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ *
17+ */
18+
19+ package org .apache .skywalking .banyandb .v1 .client ;
20+
21+ import org .apache .skywalking .banyandb .common .v1 .BanyandbCommon ;
22+ import org .apache .skywalking .banyandb .v1 .client .grpc .exception .UnauthenticatedException ;
23+ import org .junit .Assert ;
24+ import org .junit .Rule ;
25+ import org .junit .Test ;
26+ import org .testcontainers .containers .GenericContainer ;
27+ import org .testcontainers .containers .wait .strategy .Wait ;
28+ import org .testcontainers .utility .DockerImageName ;
29+ import org .testcontainers .utility .MountableFile ;
30+
31+ import java .io .IOException ;
32+ import java .nio .file .Files ;
33+ import java .nio .file .Path ;
34+ import java .nio .file .Paths ;
35+ import java .nio .file .attribute .PosixFilePermissions ;
36+ import java .util .List ;
37+ import java .util .concurrent .TimeUnit ;
38+
39+ import static org .awaitility .Awaitility .await ;
40+ import static org .junit .Assert .assertThrows ;
41+
42+ public class ITBanyanDBAuthTest {
43+ private static final String REGISTRY = "ghcr.io" ;
44+ private static final String IMAGE_NAME = "apache/skywalking-banyandb" ;
45+ private static final String TAG = "42ec9df7457868926eb80157b36355d94fcd6bba" ;
46+
47+ private static final String IMAGE = REGISTRY + "/" + IMAGE_NAME + ":" + TAG ;
48+
49+ protected static final int GRPC_PORT = 17912 ;
50+ protected static final int HTTP_PORT = 17913 ;
51+
52+ @ Rule
53+ public GenericContainer <?> banyanDB ;
54+
55+ public ITBanyanDBAuthTest () throws Exception {
56+ // Step 1: prepare config file with 0600 permissions
57+ Path tempConfigPath = Files .createTempFile ("bydb_server_config" , ".yaml" );
58+ Files .write (tempConfigPath , Files .readAllBytes (
59+ Paths .get (getClass ().getClassLoader ().getResource ("config.yaml" ).toURI ()))
60+ );
61+ Files .setPosixFilePermissions (tempConfigPath , PosixFilePermissions .fromString ("rw-------" ));
62+
63+ // Step 2: create container
64+ banyanDB = new GenericContainer <>(DockerImageName .parse (IMAGE ))
65+ .withCopyFileToContainer (
66+ MountableFile .forHostPath (tempConfigPath ),
67+ "/tmp/bydb_server_config.yaml"
68+ )
69+ .withCommand ("standalone" ,
70+ "--auth-config-file" , "/tmp/bydb_server_config.yaml"
71+ )
72+ .withExposedPorts (GRPC_PORT , HTTP_PORT )
73+ .waitingFor (Wait .forHttp ("/api/healthz" ).forPort (HTTP_PORT ));
74+ }
75+
76+ @ Test
77+ public void testAuthWithCorrect () throws IOException {
78+ BanyanDBClient client = createClient ("admin" , "123456" );
79+ client .connect ();
80+ await ().atMost (10 , TimeUnit .SECONDS ).untilAsserted (() -> {
81+ // get api version
82+ client .getAPIVersion ();
83+ // list all groups
84+ List <BanyandbCommon .Group > groupList = client .findGroups ();
85+ Assert .assertEquals (0 , groupList .size ());
86+ });
87+ client .close ();
88+ }
89+
90+ @ Test
91+ public void testAuthWithWrong () throws IOException {
92+ BanyanDBClient client = createClient ("admin" , "123456" + "wrong" );
93+ client .connect ();
94+ await ().atMost (10 , TimeUnit .SECONDS ).untilAsserted (() -> {
95+ assertThrows (UnauthenticatedException .class , client ::getAPIVersion );
96+ });
97+ client .close ();
98+ }
99+
100+ private BanyanDBClient createClient (String username , String password ) {
101+ Options options = new Options ();
102+ options .setUsername (username );
103+ options .setPassword (password );
104+ String url = String .format ("%s:%d" , banyanDB .getHost (), banyanDB .getMappedPort (GRPC_PORT ));
105+ return new BanyanDBClient (new String []{url }, options );
106+ }
107+ }
0 commit comments