Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

feat(security): init kerberos #585

Merged
merged 37 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0b99fdb
init kerberos
levy5307 Aug 5, 2020
371453d
add krb5/sasl to thirdparty/CMakeLists.txt
levy5307 Aug 5, 2020
8e2e392
change makelist
levy5307 Aug 5, 2020
6e8b03c
krb5_call_to_errors
levy5307 Aug 5, 2020
0f46110
refactor
levy5307 Aug 5, 2020
fb99365
refactor
levy5307 Aug 6, 2020
dbae3e1
fix
levy5307 Aug 6, 2020
9625159
fix
levy5307 Aug 6, 2020
48f9d0e
Merge branch 'master' into kerberos-init
levy5307 Aug 6, 2020
8c240c7
fix
levy5307 Aug 6, 2020
bbd0f28
fix
levy5307 Aug 6, 2020
102f8e9
fix
levy5307 Aug 6, 2020
dd6a2ae
fix
levy5307 Aug 6, 2020
fa1285c
fix
levy5307 Aug 7, 2020
66c44d8
Merge branch 'master' into kerberos-init
levy5307 Aug 7, 2020
3052fd9
refactor kerberos utils
levy5307 Aug 7, 2020
bbcb249
fix
levy5307 Aug 7, 2020
2d70d13
Merge branch 'kerberos-init' of github.com:levy5307/rdsn into kerbero…
levy5307 Aug 7, 2020
6a3b1af
change sleep_after_init to is_server
levy5307 Aug 7, 2020
bc898b4
fix
levy5307 Aug 7, 2020
7c6d38d
fix
levy5307 Aug 7, 2020
1b1f12d
fix
levy5307 Aug 7, 2020
da94b16
fix
levy5307 Aug 7, 2020
5767e5e
fix
levy5307 Aug 7, 2020
a41bbe8
Merge branch 'master' into kerberos-init
levy5307 Aug 14, 2020
b4fa990
Merge branch 'master' into kerberos-init
hycdong Aug 14, 2020
ac54c07
fix
levy5307 Aug 14, 2020
e6c7f75
fix
levy5307 Aug 14, 2020
d18f370
refactor
levy5307 Aug 14, 2020
ce71a2a
Merge branch 'master' into kerberos-init
levy5307 Aug 18, 2020
ea0f5bf
add get_next_renew_interval
levy5307 Aug 18, 2020
8d584da
Merge remote-tracking branch 'origin/master' into kerberos-init
levy5307 Aug 19, 2020
f4f1d5d
refactor
levy5307 Aug 19, 2020
ee19b83
refactor
levy5307 Aug 19, 2020
c55ab09
delete the wrong comment
levy5307 Aug 19, 2020
6e60cc3
Merge branch 'kerberos-init' of github.com:levy5307/rdsn into kerbero…
levy5307 Aug 19, 2020
5b96be6
Merge remote-tracking branch 'origin/master' into kerberos-init
levy5307 Aug 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/dsn/c/app_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ extern DSN_API bool dsn_mimic_app(const char *app_role, int index);
start the system with given configuration
\param config the configuration file for this run
\param sleep_after_init whether to sleep after rDSN initialization, default is false
\param is_server whether it is server or not, default is false
\return true if it succeeds, false if it fails.
*/
extern DSN_API bool dsn_run_config(const char *config, bool sleep_after_init DEFAULT(false));
extern DSN_API bool dsn_run_config(const char *config, bool is_server DEFAULT(false));

/*!
start the system with given arguments
\param argc argc in C main convention
\param argv argv in C main convention
\param sleep_after_init whether to sleep after rDSN initialization, default is false
\param is_server whether it is server or not, default is false
\return true if it succeeds, false if it fails.
Expand All @@ -89,7 +89,7 @@ extern DSN_API bool dsn_run_config(const char *config, bool sleep_after_init DEF
Note the argc, argv folllows the C main convention that argv[0] is the executable name.
*/
extern DSN_API void dsn_run(int argc, char **argv, bool sleep_after_init DEFAULT(false));
extern DSN_API void dsn_run(int argc, char **argv, bool is_server DEFAULT(false));

/*!
exit the process with the given exit code
Expand Down
1 change: 1 addition & 0 deletions include/dsn/utility/error_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ DEFINE_ERR_CODE(ERR_ZOOKEEPER_OPERATION)
DEFINE_ERR_CODE(ERR_CHILD_REGISTERED)
DEFINE_ERR_CODE(ERR_INGESTION_FAILED)

levy5307 marked this conversation as resolved.
Show resolved Hide resolved
DEFINE_ERR_CODE(ERR_KRB5_INTERNAL)
} // namespace dsn
14 changes: 13 additions & 1 deletion include/dsn/utility/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ inline void time_ms_to_date(uint64_t ts_ms, char *str, int len)
strftime(str, len, "%Y-%m-%d", get_localtime(ts_ms, &tmp));
}

// get date string with format of 'yyyy-MM-dd hh:mm:ss' from given timestamp
// get date string with format of 'yyyy-MM-dd hh:mm:ss' from given timestamp(ms)
inline void time_ms_to_date_time(uint64_t ts_ms, char *str, int len)
{
struct tm tmp;
strftime(str, len, "%Y-%m-%d %H:%M:%S", get_localtime(ts_ms, &tmp));
}

levy5307 marked this conversation as resolved.
Show resolved Hide resolved
// get date string with format of 'yyyy-MM-dd hh:mm:ss' from given timestamp(s)
inline std::string time_s_to_date_time(uint64_t unix_seconds)
{
char buffer[128];
utils::time_ms_to_date_time(unix_seconds * 1000, buffer, 128);
return std::string(buffer);
}

// parse hour/min/sec from the given timestamp
inline void time_ms_to_date_time(uint64_t ts_ms, int32_t &hour, int32_t &min, int32_t &sec)
{
Expand All @@ -78,12 +86,16 @@ inline void time_ms_to_date_time(uint64_t ts_ms, int32_t &hour, int32_t &min, in
sec = ret->tm_sec;
}

// get current physical timestamp in ns
inline uint64_t get_current_physical_time_ns()
{
auto now = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
}

// get current physical timestamp in s
inline uint64_t get_current_physical_time_s() { return get_current_physical_time_ns() * 1e-9; }

// get unix timestamp of today's zero o'clock.
// eg. `1525881600` returned when called on May 10, 2018, CST
inline int64_t get_unix_sec_today_midnight()
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ add_library(dsn_runtime STATIC
tracer.cpp
zlocks.cpp
)
target_link_libraries(dsn_runtime dsn_utils)
target_link_libraries(dsn_runtime dsn_utils sasl2 gssapi_krb5 krb5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So don't forget to update docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which docs do you mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hycdong marked this conversation as resolved.
Show resolved Hide resolved
install(TARGETS dsn_runtime DESTINATION "lib")
63 changes: 63 additions & 0 deletions src/runtime/security/init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "kinit_context.h"

#include <dsn/dist/fmt_logging.h>

namespace dsn {
namespace security {
extern char *FLAGS_krb5_config;
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
extern char *FLAGS_krb5_keytab;

/***
* set kerberos envs(for more details:
* https://web.mit.edu/kerberos/krb5-1.12/doc/admin/env_variables.html)
*/
void set_krb5_env(bool is_server)
{
setenv("KRB5CCNAME", is_server ? "MEMORY:pegasus-server" : "MEMORY:pegasus-client", 1);
setenv("KRB5_CONFIG", FLAGS_krb5_config, 1);
setenv("KRB5_KTNAME", FLAGS_krb5_keytab, 1);
setenv("KRB5RCACHETYPE", "none", 1);
}

error_s init_kerberos(bool is_server)
{
// set kerberos env
set_krb5_env(is_server);

// kinit -k -t <keytab_file> <principal>
return kinit_context::instance().kinit();
}

bool init(bool is_server)
{
error_s err = init_kerberos(is_server);
if (!err.is_ok()) {
derror_f("initialize kerberos failed, with err = {}", err.description());
return false;
}
ddebug("initialize kerberos succeed");

// TODO(zlw): init sasl

return true;
}

} // namespace security
} // namespace dsn
27 changes: 27 additions & 0 deletions src/runtime/security/init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include <dsn/utility/errors.h>

namespace dsn {
namespace security {
// init security(kerberos and sasl)
bool init(bool is_server);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool init(bool is_server);
extern bool init_server();
extern bool init_client();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no need to split the function into two functions. Because they are almost same

} // namespace security
} // namespace dsn
Loading