10
0
Fork 0
mirror of https://github.com/ZeusWPI/ZNS.git synced 2024-10-30 05:24:26 +01:00

fix all clippy warnings

This commit is contained in:
Xander Bil 2024-08-22 17:06:05 +02:00
parent 58ad3ca707
commit 81457941ac
No known key found for this signature in database
GPG key ID: EC9706B54A278598
10 changed files with 33 additions and 37 deletions

View file

@ -48,7 +48,7 @@ struct RSAKeyPair {
enum KeyPair { enum KeyPair {
ED255519(Ed25519KeyPair), ED255519(Ed25519KeyPair),
RSA(RSAKeyPair), Rsa(RSAKeyPair),
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -63,7 +63,7 @@ fn read_string(reader: &mut Reader) -> Result<String, ZNSError> {
let length = reader.read_u32()?; let length = reader.read_u32()?;
let data = reader.read(length as usize)?; let data = reader.read(length as usize)?;
let result = from_utf8(&data).map_err(|e| ZNSError::Key { let result = from_utf8(&data).map_err(|e| ZNSError::Key {
message: format!("Wrong ciphername format: {}", e.to_string()), message: format!("Wrong ciphername format: {}", e),
})?; })?;
Ok(result.to_owned()) Ok(result.to_owned())
} }
@ -190,7 +190,7 @@ impl KeyTransformer for OpenSSHKey {
let buf = reader.read(14)?; let buf = reader.read(14)?;
let magic = from_utf8(&buf).map_err(|e| ZNSError::Key { let magic = from_utf8(&buf).map_err(|e| ZNSError::Key {
message: format!("Not valid ASCII: {}", e.to_string()), message: format!("Not valid ASCII: {}", e),
})?; })?;
if magic != "openssh-key-v1" { if magic != "openssh-key-v1" {
@ -232,7 +232,7 @@ impl KeyTransformer for OpenSSHKey {
let keypair = match keytype.as_str() { let keypair = match keytype.as_str() {
"ssh-ed25519" => Ok(KeyPair::ED255519(Ed25519KeyPair::from_openssh(reader)?)), "ssh-ed25519" => Ok(KeyPair::ED255519(Ed25519KeyPair::from_openssh(reader)?)),
"ssh-rsa" => Ok(KeyPair::RSA(RSAKeyPair::from_openssh(reader)?)), "ssh-rsa" => Ok(KeyPair::Rsa(RSAKeyPair::from_openssh(reader)?)),
other => Err(ZNSError::Key { other => Err(ZNSError::Key {
message: format!("Invalid public keytype {}", other), message: format!("Invalid public keytype {}", other),
}), }),
@ -252,7 +252,7 @@ impl KeyTransformer for OpenSSHKey {
fn to_dnskey(&self, username: &str) -> (String, String) { fn to_dnskey(&self, username: &str) -> (String, String) {
match &self.keypair { match &self.keypair {
KeyPair::ED255519(keypair) => keypair.to_dnskey(username), KeyPair::ED255519(keypair) => keypair.to_dnskey(username),
KeyPair::RSA(keypair) => keypair.to_dnskey(username), KeyPair::Rsa(keypair) => keypair.to_dnskey(username),
} }
} }
} }
@ -272,7 +272,7 @@ fn ssh_to_dnskey(file_content: &str, username: &str) -> Result<(), Box<dyn Error
} }
let key_encoded = &file_content[OPENSSH_START.len()..file_content.len() - OPENSSH_END.len()] let key_encoded = &file_content[OPENSSH_START.len()..file_content.len() - OPENSSH_END.len()]
.replace("\n", ""); .replace('\n', "");
let bin = BASE64_STANDARD.decode(key_encoded)?; let bin = BASE64_STANDARD.decode(key_encoded)?;
let mut reader = Reader::new(&bin); let mut reader = Reader::new(&bin);
@ -282,8 +282,8 @@ fn ssh_to_dnskey(file_content: &str, username: &str) -> Result<(), Box<dyn Error
let mut file_public = File::create(format!("{}.key", FILENAME))?; let mut file_public = File::create(format!("{}.key", FILENAME))?;
let (private, public) = key.to_dnskey(username); let (private, public) = key.to_dnskey(username);
file_private.write(private.as_bytes())?; file_private.write_all(private.as_bytes())?;
file_public.write(public.as_bytes())?; file_public.write_all(public.as_bytes())?;
Ok(()) Ok(())
} }

View file

@ -27,7 +27,7 @@ impl Config {
zauth_url: env::var("ZAUTH_URL").expect("ZAUTH_URL must be set"), zauth_url: env::var("ZAUTH_URL").expect("ZAUTH_URL must be set"),
authoritative_zone: env::var("ZONE") authoritative_zone: env::var("ZONE")
.expect("ZONE must be set") .expect("ZONE must be set")
.split(".") .split('.')
.map(str::to_string) .map(str::to_string)
.collect(), .collect(),
port: env::var("ZNS_PORT") port: env::var("ZNS_PORT")

View file

@ -1,5 +1,3 @@
use std::fmt::format;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::sql_types::Text; use diesel::sql_types::Text;
use zns::{ use zns::{
@ -34,7 +32,7 @@ struct Record {
pub rdata: Vec<u8>, pub rdata: Vec<u8>,
} }
sql_function! { define_sql_function! {
fn lower(x: Text) -> Text; fn lower(x: Text) -> Text;
} }
@ -121,7 +119,7 @@ pub fn insert_into_database(rr: &RR, connection: &mut PgConnection) -> Result<()
} }
pub fn get_from_database( pub fn get_from_database(
name: &Vec<String>, name: &[String],
_type: Option<Type>, _type: Option<Type>,
class: Class, class: Class,
connection: &mut PgConnection, connection: &mut PgConnection,
@ -138,22 +136,20 @@ pub fn get_from_database(
Ok(records Ok(records
.into_iter() .into_iter()
.filter_map(|record| { .map(|record| RR {
Some(RR { name: record.name.split('.').map(str::to_string).collect(),
name: record.name.split(".").map(str::to_string).collect(),
_type: Type::from(record._type as u16), _type: Type::from(record._type as u16),
class: Class::from(record.class as u16), class: Class::from(record.class as u16),
ttl: record.ttl, ttl: record.ttl,
rdlength: record.rdlength as u16, rdlength: record.rdlength as u16,
rdata: record.rdata, rdata: record.rdata,
}) })
})
.collect()) .collect())
} }
//TODO: cleanup models //TODO: cleanup models
pub fn delete_from_database( pub fn delete_from_database(
name: &Vec<String>, name: &[String],
_type: Option<Type>, _type: Option<Type>,
class: Class, class: Class,
rdata: Option<Vec<u8>>, rdata: Option<Vec<u8>>,

View file

@ -29,8 +29,8 @@ impl ResponseHandler for Handler {
match message.get_opcode() { match message.get_opcode() {
//TODO: implement this in Opcode //TODO: implement this in Opcode
Ok(opcode) => match opcode { Ok(opcode) => match opcode {
Opcode::QUERY => QueryHandler::handle(&message, raw, connection).await, Opcode::QUERY => QueryHandler::handle(message, raw, connection).await,
Opcode::UPDATE => UpdateHandler::handle(&message, raw, connection).await, Opcode::UPDATE => UpdateHandler::handle(message, raw, connection).await,
}, },
Err(e) => Err(ZNSError::Formerr { Err(e) => Err(ZNSError::Formerr {
message: e.to_string(), message: e.to_string(),

View file

@ -30,9 +30,9 @@ impl ResponseHandler for QueryHandler {
match answers { match answers {
Ok(mut rrs) => { Ok(mut rrs) => {
if rrs.len() == 0 { if rrs.is_empty() {
rrs.extend(try_wildcard(question, connection)?); rrs.extend(try_wildcard(question, connection)?);
if rrs.len() == 0 { if rrs.is_empty() {
return Err(ZNSError::NXDomain { return Err(ZNSError::NXDomain {
domain: question.qname.join("."), domain: question.qname.join("."),
qtype: question.qtype.clone(), qtype: question.qtype.clone(),
@ -57,7 +57,7 @@ impl ResponseHandler for QueryHandler {
fn try_wildcard(question: &Question, connection: &mut PgConnection) -> Result<Vec<RR>, ZNSError> { fn try_wildcard(question: &Question, connection: &mut PgConnection) -> Result<Vec<RR>, ZNSError> {
let records = get_from_database(&question.qname, None, question.qclass.clone(), connection)?; let records = get_from_database(&question.qname, None, question.qclass.clone(), connection)?;
if records.len() > 0 || question.qname.len() == 0 { if !records.is_empty() || question.qname.is_empty() {
Ok(vec![]) Ok(vec![])
} else { } else {
let mut qname = question.qname.clone(); let mut qname = question.qname.clone();
@ -70,7 +70,7 @@ fn try_wildcard(question: &Question, connection: &mut PgConnection) -> Result<Ve
)? )?
.into_iter() .into_iter()
.map(|mut rr| { .map(|mut rr| {
rr.name = question.qname.clone(); rr.name.clone_from(&question.qname);
rr rr
}) })
.collect()) .collect())

View file

@ -14,7 +14,7 @@ use super::{dnskey::DNSKeyRData, sig::Sig};
pub async fn authenticate( pub async fn authenticate(
sig: &Sig, sig: &Sig,
zone: &Vec<String>, zone: &[String],
connection: &mut PgConnection, connection: &mut PgConnection,
) -> Result<bool, ZNSError> { ) -> Result<bool, ZNSError> {
if zone.len() >= Config::get().authoritative_zone.len() { if zone.len() >= Config::get().authoritative_zone.len() {
@ -53,7 +53,7 @@ async fn validate_ssh(username: &String, sig: &Sig) -> Result<bool, reqwest::Err
.json::<Vec<String>>() .json::<Vec<String>>()
.await? .await?
.iter() .iter()
.any(|key| match sig.verify_ssh(&key) { .any(|key| match sig.verify_ssh(key) {
Ok(value) => value, Ok(value) => value,
Err(e) => { Err(e) => {
eprintln!("{}", e); eprintln!("{}", e);
@ -63,7 +63,7 @@ async fn validate_ssh(username: &String, sig: &Sig) -> Result<bool, reqwest::Err
} }
async fn validate_dnskey( async fn validate_dnskey(
zone: &Vec<String>, zone: &[String],
sig: &Sig, sig: &Sig,
connection: &mut PgConnection, connection: &mut PgConnection,
) -> Result<bool, ZNSError> { ) -> Result<bool, ZNSError> {

View file

@ -96,7 +96,7 @@ impl ResponseHandler for UpdateHandler {
for rr in &message.authority { for rr in &message.authority {
if rr.class == zone.qclass { if rr.class == zone.qclass {
let _ = insert_into_database(&rr, connection)?; insert_into_database(rr, connection)?;
} else if rr.class == Class::Class(RRClass::ANY) { } else if rr.class == Class::Class(RRClass::ANY) {
if rr._type == Type::Type(RRType::ANY) { if rr._type == Type::Type(RRType::ANY) {
if rr.name == zone.qname { if rr.name == zone.qname {

View file

@ -19,7 +19,7 @@ pub trait PublicKey {
let algo_type = from_utf8(&read).map_err(|e| ZNSError::Key { let algo_type = from_utf8(&read).map_err(|e| ZNSError::Key {
message: format!( message: format!(
"Could not convert type name bytes to string: {}", "Could not convert type name bytes to string: {}",
e.to_string() e
), ),
})?; })?;

View file

@ -51,7 +51,7 @@ async fn get_response(bytes: &[u8]) -> Vec<u8> {
response response
} }
Err(e) => { Err(e) => {
eprintln!("{}", e.to_string()); eprintln!("{}", e);
message.set_response(e.rcode()); message.set_response(e.rcode());
message message
} }

View file

@ -2,8 +2,8 @@ use crate::structs::{Message, Opcode, RCODE};
impl Message { impl Message {
pub fn set_response(&mut self, rcode: RCODE) { pub fn set_response(&mut self, rcode: RCODE) {
self.header.flags = (self.header.flags | 0b1_0000_1_0_0_0_000_0000 | rcode as u16) self.header.flags =
& 0b1_1111_1_0_1_0_111_1111 (self.header.flags | 0b1000_0100_0000_0000 | rcode as u16) & 0b1111_1101_0111_1111
} }
pub fn get_opcode(&self) -> Result<Opcode, String> { pub fn get_opcode(&self) -> Result<Opcode, String> {